Creation of GLCustom model is slightly more RAII

This commit is contained in:
Aaron Culliney 2016-01-03 11:57:48 -08:00
parent 2ec88ad433
commit 9219fa7053
6 changed files with 85 additions and 99 deletions

View File

@ -26,8 +26,9 @@ static GLModel *messageModel = NULL;
// ----------------------------------------------------------------------------
static void *_create_alert(void) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)glhud_createDefault();
static void *_create_alert(GLModel *parent) {
parent->custom = glhud_createDefault();
GLModelHUDElement *hudElement = (GLModelHUDElement *)parent->custom;
if (hudElement) {
hudElement->colorScheme = RED_ON_BLACK;
hudElement->blackIsTransparent = false;
@ -71,7 +72,6 @@ static void _alertToModel(char *message, unsigned int messageCols, unsigned int
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but texture (message pixels) does
}, (GLCustom){
.create = &_create_alert,
.setup = NULL,
.destroy = &glhud_destroyDefault,
});
if (!messageModel) {

View File

@ -74,7 +74,7 @@ static struct {
// ----------------------------------------------------------------------------
static void _setup_axis_object(GLModel *parent) {
static void _setup_axis_hud(GLModel *parent) {
if (UNLIKELY(!parent)) {
LOG("gltouchjoy WARN : cannot setup axis object without parent");
return;
@ -116,15 +116,17 @@ static void _setup_axis_object(GLModel *parent) {
glhud_setupDefault(parent);
}
static void *_create_touchjoy_hud(void) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)glhud_createDefault();
static void *_create_axis_hud(GLModel *parent) {
parent->custom = glhud_createDefault();
GLModelHUDElement *hudElement = (GLModelHUDElement *)parent->custom;
if (hudElement) {
hudElement->blackIsTransparent = true;
_setup_axis_hud(parent);
}
return hudElement;
}
static void _setup_button_object(GLModel *parent) {
static void _setup_button_hud(GLModel *parent) {
if (UNLIKELY(!parent)) {
LOG("gltouchjoy WARN : cannot setup button object without parent");
return;
@ -159,10 +161,20 @@ static void _setup_button_object(GLModel *parent) {
glhud_setupDefault(parent);
}
static void *_create_button_hud(GLModel *parent) {
parent->custom = glhud_createDefault();
GLModelHUDElement *hudElement = (GLModelHUDElement *)parent->custom;
if (hudElement) {
hudElement->blackIsTransparent = true;
_setup_button_hud(parent);
}
return hudElement;
}
static inline void _setup_button_object_with_char(char newChar) {
if (buttons.activeChar != newChar) {
buttons.activeChar = newChar;
_setup_button_object(buttons.model);
_setup_button_hud(buttons.model);
}
}
@ -199,8 +211,7 @@ static void gltouchjoy_setup(void) {
.tex_h = AXIS_FB_HEIGHT,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // so can texture
}, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_axis_object,
.create = &_create_axis_hud,
.destroy = &glhud_destroyDefault,
});
if (!axes.model) {
@ -225,8 +236,7 @@ static void gltouchjoy_setup(void) {
.tex_h = BUTTON_FB_HEIGHT,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // so can texture
}, (GLCustom){
.create = &_create_touchjoy_hud,
.setup = &_setup_button_object,
.create = &_create_button_hud,
.destroy = &glhud_destroyDefault,
});
if (!buttons.model) {
@ -707,7 +717,7 @@ static touchjoy_variant_t gltouchjoy_getTouchVariant(void) {
static void gltouchjoy_setTouchAxisTypes(uint8_t rosetteChars[(ROSETTE_ROWS * ROSETTE_COLS)], int rosetteScancodes[(ROSETTE_ROWS * ROSETTE_COLS)]) {
memcpy(axes.rosetteChars, rosetteChars, sizeof(uint8_t)*(ROSETTE_ROWS * ROSETTE_COLS));
memcpy(axes.rosetteScancodes, rosetteScancodes, sizeof(int) *(ROSETTE_ROWS * ROSETTE_COLS));
_setup_axis_object(axes.model);
_setup_axis_hud(axes.model);
}
static void gltouchjoy_setScreenDivision(float screenDivider) {

View File

@ -43,10 +43,6 @@
#define KBD_OBJ_W 2.0
#define KBD_OBJ_H 2.0
HUD_CLASS(GLModelHUDKeyboard,
// ...
);
static bool isAvailable = false; // Were there any OpenGL/memory errors on gltouchkbd initialization?
static bool isEnabled = true; // Does player want touchkbd enabled?
static bool ownsScreen = false; // Does the touchkbd currently own the screen to the exclusion?
@ -142,7 +138,7 @@ static struct {
#warning FIXME TODO ... make this a generic GLModelHUDElement function
static void _rerender_character(int col, int row) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)(kbd.model->custom);
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)(kbd.model->custom);
// In English, this changes one glyph within the keyboard texture data to be the (un)selected color. It handles
// scaling from indexed color to RGBA8888 (4x) and then possibly scaling to 2x or greater.
@ -187,7 +183,7 @@ static inline void _rerender_selected(int col, int row) {
_rerender_character(col, row);
// rerender certain adjacent keys ...
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)(kbd.model->custom);
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)(kbd.model->custom);
const unsigned int indexRow = (hudKeyboard->tplWidth+1) * row;
uint8_t key = (hudKeyboard->tpl+indexRow)[col];
switch (key) {
@ -216,7 +212,7 @@ static inline void _rerender_selected(int col, int row) {
}
static inline void _switch_keyboard(GLModel *parent, uint8_t *template) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)parent->custom;
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)parent->custom;
memcpy(hudKeyboard->tpl, template, sizeof(kbdTemplateUCase/* assuming all the same size */));
// setup normal color pixels
@ -242,7 +238,7 @@ static inline bool _is_point_on_keyboard(float x, float y) {
}
static inline void _screen_to_keyboard(float x, float y, OUTPARM int *col, OUTPARM int *row) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)(kbd.model->custom);
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)(kbd.model->custom);
const unsigned int keyW = touchport.kbdW / hudKeyboard->tplWidth;
const unsigned int keyH = touchport.kbdH / hudKeyboard->tplHeight;
@ -261,7 +257,7 @@ static inline void _screen_to_keyboard(float x, float y, OUTPARM int *col, OUTPA
}
static inline int64_t _tap_key_at_point(float x, float y) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)kbd.model->custom;
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)kbd.model->custom;
// redraw previous selected key (if any)
_rerender_selected(kbd.selectedCol, kbd.selectedRow);
@ -324,6 +320,7 @@ static inline int64_t _tap_key_at_point(float x, float y) {
scancode = SCODE_L_CTRL;
break;
case MOUSETEXT_RETURN:
case ICONTEXT_RETURN_L:
case ICONTEXT_RETURN_R:
key = ICONTEXT_RETURN_L;
@ -429,8 +426,18 @@ static inline int64_t _tap_key_at_point(float x, float y) {
// ----------------------------------------------------------------------------
// GLCustom functions
static void _setup_touchkbd_hud(GLModel *parent) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)parent->custom;
static void *_create_touchkbd_hud(GLModel *parent) {
parent->custom = glhud_createCustom(sizeof(GLModelHUDElement));
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)parent->custom;
if (!hudKeyboard) {
return NULL;
}
hudKeyboard->blackIsTransparent = true;
hudKeyboard->opaquePixelHalo = true;
hudKeyboard->glyphMultiplier = kbd.glyphMultiplier;
hudKeyboard->tplWidth = KBD_TEMPLATE_COLS;
hudKeyboard->tplHeight = KBD_TEMPLATE_ROWS;
@ -445,27 +452,12 @@ static void _setup_touchkbd_hud(GLModel *parent) {
// setup normal color pixels
hudKeyboard->colorScheme = RED_ON_BLACK;
glhud_setupDefault(parent);
}
static void *_create_touchkbd_hud(void) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)glhud_createCustom(sizeof(GLModelHUDKeyboard));
if (hudKeyboard) {
hudKeyboard->blackIsTransparent = true;
hudKeyboard->opaquePixelHalo = true;
hudKeyboard->glyphMultiplier = kbd.glyphMultiplier;
}
return hudKeyboard;
}
static void _destroy_touchkbd_hud(GLModel *parent) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)parent->custom;
if (!hudKeyboard) {
return;
}
glhud_destroyDefault(parent);
}
// ----------------------------------------------------------------------------
// GLNode functions
@ -502,8 +494,7 @@ static void gltouchkbd_setup(void) {
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but key texture does
}, (GLCustom){
.create = &_create_touchkbd_hud,
.setup = &_setup_touchkbd_hud,
.destroy = &_destroy_touchkbd_hud,
.destroy = &glhud_destroyDefault,
});
if (!kbd.model) {
LOG("gltouchkbd initialization problem");
@ -657,7 +648,7 @@ static void gltouchkbd_setTouchKeyboardOwnsScreen(bool pwnd) {
kbd.selectedRow = -1;
if (kbd.model) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)kbd.model->custom;
GLModelHUDElement *hudKeyboard = (GLModelHUDElement *)kbd.model->custom;
hudKeyboard->colorScheme = RED_ON_BLACK;
glhud_setupDefault(kbd.model);
}

View File

@ -29,10 +29,6 @@
#define MENU_OBJ_W 2.0
#define MENU_OBJ_H 0.5 // NOTE : intent is to complement touch keyboard height
HUD_CLASS(GLModelHUDMenu,
uint8_t *pixelsAlt; // alternate color pixels
);
static bool isAvailable = false; // Were there any OpenGL/memory errors on initialization?
static bool isEnabled = true; // Does player want this enabled?
@ -85,15 +81,9 @@ static struct {
// ----------------------------------------------------------------------------
static inline void _present_menu(GLModel *parent) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)parent->custom;
GLModelHUDElement *hudMenu = (GLModelHUDElement *)parent->custom;
memcpy(hudMenu->tpl, topMenuTemplate, sizeof(topMenuTemplate));
// setup the alternate color (AKA selected) pixels
hudMenu->colorScheme = GREEN_ON_BLACK;
glhud_setupDefault(parent);
memcpy(hudMenu->pixelsAlt, hudMenu->pixels, (hudMenu->pixWidth * hudMenu->pixHeight));
// setup normal color pixels
hudMenu->colorScheme = RED_ON_BLACK;
glhud_setupDefault(parent);
}
@ -164,7 +154,7 @@ static inline bool _is_point_on_right_menu(float x, float y) {
#warning FIXME TODO : make this function generic _screen_to_model() ?
static inline void _screen_to_menu(float x, float y, OUTPARM int *col, OUTPARM int *row) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)(menu.model->custom);
GLModelHUDElement *hudMenu = (GLModelHUDElement *)(menu.model->custom);
const unsigned int keyW = touchport.width / hudMenu->tplWidth;
const unsigned int keyH = touchport.topLeftYMax / hudMenu->tplHeight;
@ -304,8 +294,17 @@ static inline int64_t _tap_menu_item(float x, float y) {
// ----------------------------------------------------------------------------
// GLCustom functions
static void _setup_touchmenu(GLModel *parent) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)parent->custom;
static void *_create_touchmenu_hud(GLModel *parent) {
parent->custom = glhud_createCustom(sizeof(GLModelHUDElement));
GLModelHUDElement *hudMenu = (GLModelHUDElement *)parent->custom;
if (!hudMenu) {
return NULL;
}
hudMenu->blackIsTransparent = true;
hudMenu->opaquePixelHalo = true;
hudMenu->glyphMultiplier = menu.glyphMultiplier;
hudMenu->tplWidth = MENU_TEMPLATE_COLS;
hudMenu->tplHeight = MENU_TEMPLATE_ROWS;
@ -331,30 +330,12 @@ static void _setup_touchmenu(GLModel *parent) {
const unsigned int size = sizeof(topMenuTemplate);
hudMenu->tpl = CALLOC(size, 1);
hudMenu->pixels = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1);
hudMenu->pixelsAlt = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1);
_present_menu(parent);
}
static void *_create_touchmenu(void) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)glhud_createCustom(sizeof(GLModelHUDMenu));
if (hudMenu) {
hudMenu->blackIsTransparent = true;
hudMenu->opaquePixelHalo = true;
hudMenu->glyphMultiplier = menu.glyphMultiplier;
}
return hudMenu;
}
static void _destroy_touchmenu(GLModel *parent) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)parent->custom;
if (!hudMenu) {
return;
}
FREE(hudMenu->pixelsAlt);
glhud_destroyDefault(parent);
}
// ----------------------------------------------------------------------------
// GLNode functions
@ -389,9 +370,8 @@ static void gltouchmenu_setup(void) {
.tex_h = MENU_FB_HEIGHT * menu.glyphMultiplier,
.texcoordUsageHint = GL_DYNAMIC_DRAW, // but menu texture does
}, (GLCustom){
.create = &_create_touchmenu,
.setup = &_setup_touchmenu,
.destroy = &_destroy_touchmenu,
.create = &_create_touchmenu_hud,
.destroy = &glhud_destroyDefault,
});
if (!menu.model) {
LOG("gltouchmenu initialization problem");

View File

@ -473,7 +473,11 @@ GLModel *mdlCreateQuad(GLModelParams_s parms, GLCustom clazz) {
model->positionSize = 4; // x,y,z coordinates
model->positionArraySize = sizeof(obj_positions);
if (tex_w > 0 && tex_h > 0) {
model->texcoordType = UNINITIALIZED_GL;
model->texcoordSize = 0;
model->texcoordArraySize = 0;
const bool useTexture = (tex_w > 0 && tex_h > 0);
if (useTexture) {
model->texCoords = MALLOC(sizeof(obj_texcoords));
if (!(model->texCoords)) {
break;
@ -508,9 +512,10 @@ GLModel *mdlCreateQuad(GLModelParams_s parms, GLCustom clazz) {
model->posBufferName = UNINITIALIZED_GL;
model->texcoordBufferName = UNINITIALIZED_GL;
model->elementBufferName = UNINITIALIZED_GL;
model->texFormat = UNINITIALIZED_GL;
_quadCreateVAOAndVBOs(model);
if (model->posBufferName == UNINITIALIZED_GL || model->texcoordBufferName == UNINITIALIZED_GL || model->elementBufferName == UNINITIALIZED_GL) {
if (model->posBufferName == UNINITIALIZED_GL || (useTexture && model->texcoordBufferName == UNINITIALIZED_GL) || model->elementBufferName == UNINITIALIZED_GL) {
LOG("Error creating model buffers!");
break;
}
@ -518,28 +523,28 @@ GLModel *mdlCreateQuad(GLModelParams_s parms, GLCustom clazz) {
model->texDirty = true;
model->texWidth = tex_w;
model->texHeight = tex_h;
model->texFormat = TEX_FORMAT;
model->texPixels = (GLvoid *)MALLOC(tex_w * tex_h * sizeof(PIXEL_TYPE));
if (model->texPixels == NULL) {
break;
}
model->textureName = _quadCreateTexture(model);
if (model->textureName == UNINITIALIZED_GL) {
LOG("Error creating model texture!");
break;
if (useTexture) {
model->texFormat = TEX_FORMAT;
model->texPixels = (GLvoid *)MALLOC(tex_w * tex_h * sizeof(PIXEL_TYPE));
if (model->texPixels == NULL) {
break;
}
model->textureName = _quadCreateTexture(model);
if (model->textureName == UNINITIALIZED_GL) {
LOG("Error creating model texture!");
break;
}
}
model->custom = NULL;
if (clazz.create) {
model->custom = clazz.create();
if (model->custom) {
model->custom->create = NULL;
model->custom->setup = clazz.setup;
model->custom->destroy = clazz.destroy;
if (model->custom->setup) {
model->custom->setup(model);
}
model->custom = clazz.create(model);
if (!model->custom) {
LOG("Error creating custom model!");
break;
}
model->custom->create = NULL;
model->custom->destroy = clazz.destroy;
}
GL_ERRLOG("quad creation");
@ -627,6 +632,7 @@ void mdlDestroyModel(INOUT GLModel **model) {
if (m->custom && m->custom->destroy) {
m->custom->destroy(m);
m->custom = NULL;
}
FREE(*model);

View File

@ -28,8 +28,7 @@ typedef struct GLModel;
#define MODEL_CLASS(CLS, ...) \
typedef struct CLS { \
void *(*create)(void); \
void (*setup)(struct GLModel *parent); \
void *(*create)(struct GLModel *parent); \
void (*destroy)(struct GLModel *parent); \
__VA_ARGS__ \
} CLS;