I'm developing a little game engine for Android with Android NDK and opengl es 2.0, recently the project is getting big, and I need to refactor some code, and I couldn't find a proper design pattern for the next problem.
On android when the app reach the OnPause() state the opengl context is destroyed, but the state of the variables and objects in java and c++ are maintained. so each time the player pauses and resumes the app I have to reinitializate the opengl part, buffers, shaders, vertex, etc.
I have classes like "Square" that makes "square objects", and each one has its own attributes, and each "square object" can be drawn, so the squares can access to static (opengl) members of the class, that are used to be properly rendered. So this static members must be initialized before objects can be drawn, I do it when the opengl context is created or recreated.
moreover each class has its own opengl attributes, so each class is initialized individually with its own parameters, so I want a design in what each class can set some initial parameters, pass or catch those parameters to initialize the static members of the class (I forgot to say that these parameters are private). But as I said before, these parameters need to be reinitialized each time the app is resumed.
currently I initialize these members individually like
Square::init(/*hardcoded parameters*/);
Circle::init(/*hardcoded parameters*/);
Triangle::init(/*hardcoded parameters*/);
Polygon::init(/*hardcoded parameters*/);
Shape::init(/*hardcoded parameters*/);
.
.
.
.
// Many other inits.....
.
and I want to write something like
// here all the classes with opengl part are initialized
// all init methods of each class are called here, with their respective parameters
Opengl_Initializer::init(); // <--- magic way, no other init calls
So I want to set some (static/harcoded) variables to the class and then when the opengl context be created, the class be initialized in a "magic" way, and not having the need to code the call to an init method for each class.
I've tried to use inheritance, but the issue is that I need to initialize the class not the object, also tried to implement a static object and initialize this object in the cpp file, and store a pointer to the object in a vector when this is created in his contructor, in a vector that is in the object's own class, but this design has gave me many problems.
Does anyone know some design that can help me?
EDIT: the stucture of my classes
the init() function is really big because shader and frag parameters are paths file and I perform some task on them, pass the result of that perform to opengl and returns me a ID that is the program static variable, all clases with opengl part implement this same process, the parameter camera is just to attach it into a camera
class Square {
// static variable all classes have
static GLuint program;
// other glparameters not in common, initialized in the same static init() method
static GLint uniform1;
static GLint uniform2;
public;
// the static init function has the same header on all the classes
static init(const char* shader, const char* frag, const char *camera);
}
and maybe some structure I'd want is
class Square {
static GLuint program;
static const char *vertex = "hardcode";
static const char *frag = "hardcode";
static const char *cam = "harcode";
static init();
/// or somethig like
static Initializer init(
"harcode shader", "hardcode frag", "hardcode camera",
[&] (void) ->void {
//this is the init function
}
);
public:
}
This is one more solution how your task can be solved. The idea is to have some initialization list (std::vector) of functions that should be called in yout Opengl_Initializer::init() :
std::vector<std::function<void()>> initializer_list;
If we can put all your Square/Circle/Triangle... init functions into this list, your task become trivial - just iterate list and call all functions:
// inside Opengl_Initializer::init()
for (auto fn : initializer_list)
fn();
You can add functions manually, for example, from int main():
initializer_list.push_back(&Square::init);
...
But I suggest that you need some arhitecture design that will make you able adding functions into initializer list without changing main or any other global code.
To solve this task we can make small helper class that will register your init functions automatically:
struct OpenGLHelper_initializer
{
OpenGLHelper_initializer(std::function<void()> fn)
{
initializer_list.push_back(fn);
}
};
So you can declare instance of this class in your Square/Circle:
struct Square
{
static OpenGLHelper_initializer __initializer;
};
And in your Square.cpp file:
OpenGLHelper_initializer Square::__initializer(&Square::init);
So, when program loads, all this initializer will be constructed and all your "init" function will be registered into initializer_list.
This looks like more code, but it will make you able to add as many shapes as you need without changing Opengl_Initializer::init(); or main.cpp or any other global code
Your can now remove init functions, if you dont like them and use lambdas:
// in square.cpp
OpenGLHelper_initializer Square::__initializer([](){
std::cout << "Square is initialized now" << std::endl;
});
Here is complete source code (Updated with using static function) (but without cpp files - all in one):
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
/////////////////////////////////////////
// opengl_helper.h
// this is some manager class that knows what should be initialized later
struct OpenGLHelper
{
typedef std::function<void()> function_type;
static std::vector<function_type>& get_initialization_list();
static void register_initializer(function_type fn);
static void run_init();
};
// helper class that will register some function at construction time
struct OpenGLHelper_initializer
{
OpenGLHelper_initializer(OpenGLHelper::function_type fn)
{
OpenGLHelper::register_initializer(fn);
}
};
/////////////////////////////////////////
//opengl_helper.cpp
// using this function we will make our initializer_list be constructued
// before adding anything into it
std::vector<OpenGLHelper::function_type>& OpenGLHelper::get_initialization_list()
{
static std::vector<function_type> initializer_list;
return initializer_list;
}
// function that puts initializer into a list.
void OpenGLHelper::register_initializer(OpenGLHelper::function_type fn)
{
get_initialization_list().push_back(fn);
}
void OpenGLHelper::run_init()
{
for (auto fn : get_initialization_list())
fn();
}
/////////////////////////////////////////
// figure.h
// here is sample class that will be registered for initialization
struct Square
{
static int to_be_initialized;
// static member that will register Square class to be initialized
static OpenGLHelper_initializer __initializer;
};
/////////////////////////////////////////
// Square.cpp
int Square::to_be_initialized = 0;
// this is the most interesting part - register square into initializer list
OpenGLHelper_initializer Square::__initializer([](){
Square::to_be_initialized = 15;
std::cout << "Called Square::init: " << to_be_initialized << std::endl;
});
int main()
{
std::cout << "Before initialization : " << Square::to_be_initialized << std::endl;
OpenGLHelper::run_init();
std::cout << "After initialization : " << Square::to_be_initialized << std::endl;
return 0;
}
Output:
Before initialization : 0
Called Square::init: 15
After initialization : 15
Live test
BTW, such way of initialization is used by QT's metatype system - it uses macros to simplify code
UPDATE:
As Ben suggested, we can eliminate small memory leak from bynamic link allocation if we will put initialization list into a static function. Here is new code
I suggest a versioning system, so that initialization can be automatically performed at time-of-use, but in a way that skips it very cheaply when the initialization has already been done. Something like
int global_gl_generation = 0; // increment each time you recreate the context
inline bool check_gl_generation(int& local_generation)
{
if (local_generation == global_gl_generation)
return false;
local_generation = global_gl_generation;
return true;
}
and then in each class,
class Square
{
// static variable all classes have
static int generation_inited;
static GLuint program;
static GLint uniform1;
static GLint uniform2;
static init(const char* shader, const char* frag, const char *camera);
public;
void draw() override
{
if (check_gl_generation(generation_inited)) init(...);
// use program, uniform1, uniform2
}
};
Related
I'm trying to understand FreeRTOS building a C++ class which contains a LED blinking task. But in the task body (which is also a class member), other class members i.e. LED1_delay are empty/not-initialized. It seems like the task body was linked to another instance.
Class function which sets the blinking frequency and starts the task: (gpio.cpp)
void c_gpio::LED_blink_on(float frequency){
LED1_delay=(uint32_t)(1000/frequency);
if(LEDTaskcreated!=true){
//Create task
LEDTaskHandle = osThreadNew(startTask_LED1_blinker, LEDTask_args, &LEDTask_attributes);
LEDTaskcreated=true;
}
}
Wrapper function avoiding static declaration: (gpio.cpp)
void c_gpio::startTask_LED1_blinker(void* _this){
static_cast<c_gpio*>(_this)->taskbody_LED1_blinker((void*)0);
}
Task body: (gpio.cpp)
void c_gpio::taskbody_LED1_blinker(void* arguments){
//All class members are uninitialized here..
while(1)
{
HAL_GPIO_TogglePin(GP_LED_1_GPIO_Port,GP_LED_1_Pin);
osDelay(this->LED1_delay); //LED1_delay is not set.
}
}
Class declaration (gpio.hpp)
class c_gpio{
public:
void LED_blink_on(uint8_t LED_id, float frequency);
private:
static void startTask_LED1_blinker(void* _this);
void taskbody_LED1_blinker(void *arguments);
uint32_t LED1_delay;
//Task handles & attributes
osThreadId_t LEDTaskHandle;
osThreadAttr_t LEDTask_attributes;
uint16_t LEDTask_args[2];
};
Instantiation (main.cpp)
#include "gpio.hpp"
c_gpio gpio;
int main(void)
{
gpio.LED_blink_on(1,10);
/* Init scheduler */
osKernelInitialize();
/* Start scheduler */
osKernelStart();
}
I thought, the members taskbody_LED1_blinker() and LED1_delay are belonging to the same instance. But this doesn't seem to be so. Why? How to properly construct such a task?
Problem:
You have used the class object "gpio" for setting the blicking frequency. meaning the blinking frequency is updated inside the gpio object . Whereas "startTask_LED1_blinker" is a static method of a class which is not bound to any object
Solution:
Take advantage of the "arguments" to the task body function startTask_LED1_blinker. You can send 'this' pointer to the osThreadNew instead of LEDTask_args and then use that pointer to invoke task body "taskbody_LED1_blinker". Correction in your code for reference
void c_gpio::taskbody_LED1_blinker(void* arguments){
//All class members are uninitialized here..
while(1)
{
HAL_GPIO_TogglePin(GP_LED_1_GPIO_Port,GP_LED_1_Pin);
osDelay(LED1_delay); //LED1_delay is not set.
}
}
void c_gpio::LED_blink_on(float frequency){
LED1_delay=(uint32_t)(1000/frequency);
if(LEDTaskcreated!=true){
//Create task
LEDTaskHandle = osThreadNew(startTask_LED1_blinker, this, &LEDTask_attributes);
LEDTaskcreated=true;
}
}
I've got some trouble with static members and methods in C++.
This is the Class header:
class Decoration {
public:
//Static Methods
static void reloadList();
//Static Members
static std::unordered_map<int, Decoration> decorationMapID;
};
And in .cpp:
void Decoration::reloadList() {
sqlTable result = db->exec("SELECT id, name, description FROM decorations");
for(sqlRow r: result) {
Decoration::decorationMapID.insert(std::pair<int,Decoration>(atoi(r[0].c_str()), Decoration(r[1], r[2], atoi(r[0].c_str()))));
}
}
Now, in my mainWindow class (I'm using QT5), I call reloadList() and initialize the map.
The list is now filled in this Object.
In another Window-Class, I want to use this static list, but the list is empty. Could you explain how I have to use the static members to access the same list everywhere?
Declaration of second class:
in mainWindow.h:
ShoppingLists slDialog;
in mainWindow.cpp I call:
slDialog.setModal(true); slDialog.show();
Btw.: The whole thing is a CocktailDatabase, so my target is to have a List/Map for Cocktail-, Ingredient-, Decoration-, and Taste- Objects, which I can use without reloading it from SQLite.
1) The static member exists only once and is shared between all the instances of Decoration.
2) The question is why it is empty. Here some hints:
a) You think it is empty because some windows object was not refreshed and is not aware that your list was poupulated.
b) Your window get initiaslised before the static list is initialised
3) nevertheless an advice: don't make your static list public, especially if it has to be initialised before it's used. Use a public access function that makes sure it's initialised. Here the rough idea:
class Decoration {
public:
std::unordered_map<int, Decoration> getMap();
protected: // or even private ?
static void reloadList();
static bool loaded=false;
static std::unordered_map<int, Decoration> decorationMapID;
};
where getMap() would be something like:
if (!loaded) {
... // load the list here
loaded = true;
}
return (decorationMapID);
You are using the default-constructed value of the static variable before it had a chance to be populated. If you put breakpoints in the code that uses the value and the code that initializes it, you'll see that the latter is called after the former.
I've developed a game on Cocos2d-x v2 platform
I started on Android, after completely finishing the coding on Eclipse I used the same code on Xcode to create and iOS version.
After adding all the required libraries, I succeeded in compiling the code. However, the game hangs the moment it runs on an iOS device, although it runs without any problem on Android.
I tried both the emulator and an iPod, but I always get an EXC_BAD_ACCESS when accessing a static member from a static method. The static member would always point to 0x0!!
Here's an excerpt from the code:\
AppDelegate.cpp
#include "AppDelegate.h"
#include "NASEncData.h"
AppDelegate::AppDelegate()
{
ep = NASEncData::sharedUserData();
}
NASEncData.h
namespace CocosNas
{
class NASEncData : public CCObject
{
public:
static NASEncData* sharedUserData();
private:
NASEncData();
static void initXMLFilePath();
static std::string m_sFilePath;
}
}
NASEncData.cpp
#include "NASEncData.h"
NASEncData* NASEncData::sharedUserData()
{
initXMLFilePath();
// only create xml file one time
// the file exists after the programe exit
if ((! isXMLFileExist()) && (! createXMLFile()))
{
return NULL;
}
if (! m_spUserData)
{
m_spUserData = new NASEncData();
}
return m_spUserData;
}
void NASEncData::initXMLFilePath()
{
if (! m_sbIsFilePathInitialized)
{
m_sFilePath += CCFileUtils::sharedFileUtils()->getWriteablePath() + NASENCDATA_XML_FILE_NAME; <----error happens here
m_sbIsFilePathInitialized = true;
}
}
Based on the comments on your question, your problem could be the initialization order of static data. To be more specific, I think it is possible that the static AppDelegate instance gets created and initialized before the static members in the NASEncData. And this problem could also explain the fact that you are seeing different behavior on different platform, because the initialization order of unrelated static data are compiler and linker dependent.
To fix this, you could change your NASEncData like this: (note that this is only one possible fix, although if the initialization order of statics is really your problem, I think this is the simplest and best solution, apart from redesigning your code so that you don't have to rely on static members.)
NASEncData.h
namespace CocosNas
{
class NASEncData : public CCObject
{
public:
static NASEncData* sharedUserData();
private:
NASEncData();
static void initXMLFilePath();
// Note how the stuff below this line have changed
struct StaticData
{
std::string m_sFilePath;
// Put other static members here
// (e.g. m_sbIsFilePathInitialized, m_spUserData, etc.)
// Put a proper constructor here if needed
};
static StaticData & getStaticData ()
{
static StaticData s_data;
return s_data;
}
}
}
NASEncData.cpp
void NASEncData::initXMLFilePath()
{
if (! m_sbIsFilePathInitialized)
{
// Note the change in accessing m_sFilePath
// You should access all your static members like this
getStaticData().m_sFilePath +=
CCFileUtils::sharedFileUtils()->getWriteablePath() +
NASENCDATA_XML_FILE_NAME;
getStaticData().m_sbIsFilePathInitialized = true;
}
}
What this does in ensure that when you try and access your static member data, they have already been initialized. This happens because all your static member data are now defined inside a function as static, and for accessing them you have to call that function, and the compiler generates code to make sure that the first time that function is invoked (and only the first time) your data is constructed and initialized properly, which in turn means that the first time you actually try to access this data, whenever and wherever that may be from, your data is properly initialized.
It was indeed an initialization problem I just had to move the code from the construction to bool AppDelegate::applicationDidFinishLaunching() and it worked!
I would like to access the data within this member function that is static. Right now the member function is static so that I can use it with a third party API written in C that has typdef function pointer for callback purposes. Based on the info below, what is the best way to get around the need to create a static function in order to use the data from the following function member within other member functions of my class that are non-static. Maybe there is a way to still use this static function but still overcome the inability to mix static with non-static variables. My code does works as is but with no ability to access the data in the following callback function.
void TextDetect::vtrCB(vtrTextTrack *track, void *calldata) /*acts as a callback*/
{
/*specifically would like to take data from "track" to a deep copy so that I don't loose scope over the data withing that struct */
}
In an associated API written in C, there are the following two lines of code that I am forced to use:
typedef void (*vtrCallback)(vtrTextTrack *track, void *calldata);
int vtrInitialize(const char *inifile, vtrCallback cb, void *calldata);
Here is the header of my class:
#include <vtrapi.h>
#include <opencv.hpp>
class TextDetect {
const char * inifile;
vtrImage *vtrimage;
int framecount;
public:
TextDetect();
~TextDetect();
static void vtrCB(vtrTextTrack *track, void *calldata);
int vtrTest(cv::Mat);
bool DrawBox(cv::Mat&);
};
TextDetect::TextDetect() : inifile("vtr.ini")
{
if (vtrInitialize(inifile, vtrCB /*run into problems here*/, NULL) == -1)
std::cout << "Error: Failure to initialize" << std::endl;
vtrimage = new vtrImage;
framecount = 0;
}
void TextDetect::vtrCB(vtrTextTrack *track, void *calldata) /*acts as a callback*/
{
/*specifically would like to take data from "track" to a deep copy so that I don't loose scope over the data withing that struct */
}
I am not sure I understand your precise situation, but here is the standard idiom for wrapping a C++ method into a C callback API:
/*regular method*/
void TextDetect::vtrCB(vtrTextTrack *track)
{
// do all the real work here
}
/*static method*/
void TextDetect::vtrCB_thunk(vtrTextTrack *track, void *data)
{
static_cast<TextDetect *>(data)->vtrCB(track);
}
and then, assuming the function that should call vtrInitialize is also a TextDetect method, you write the call like this:
vtrInitialize(inifile, TextDetect::vtrCB_thunk, static_cast<void *>(this));
I have few files:
main.cpp:
int main{
...
while(1){
...
draw();
...
}
...
return 0;
}
and draw.cpp:
I want to see objects and all manipulations here.
I cant make objects local to draw(), because draw() is inside loop,
so I will get many object constructor/destructor calls - so they are global. Also
I ve made init block to prevent unnecessary calls/assignments
draw.cpp:
Object A, B;
int initialized = 0;
void draw(){
if(!initialized){
A.initialization;
B.initialization;
initialized = 1;
}
A.move(1,1);
B.rotate(45);
}
It works, but Im looking for better way to organize my code
Added:
Thanks for answers, looks like I have to read something about pattern designs
Here's steps to make it work better:
Add a struct containing all your objects near main().
pass it to draw(MyStruct &s); via reference parameter.
you're done.
Option 1
Define a new Class called Draw and put the attributes into it. You need to modify main and draw files for this. With this you can avoid declaring anything global
Option 2
Define a class within draw.cpp called draw and add your current global variables as static member variables. Initialize and use them using static functions. With this you dont have to change main.
This design technique is called Singleton (one of the design patterns)
Example code
draw.cpp
class Draw
{
public:
object A, B;
static void init()
{
// init A
// init B
isInitialized = 1;
}
static int isInitialized;
static Object & getA()
{
if(isInitialized == 0)
{
init();
}
return A;
}
// similarly B
};