Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members Related Pages
Lesson8: Using different windowing libraries.
- This is not actually a lesson, rather an explanation of how internals are set up for sjugi.
- First let me tell you what should be done if you already have your opengl running and all you need is to add a button or dialog to your program. You can visualize my gui as a layer that you throw over your graphics. You will need an instance of object that you want to use (button, dialog, so on...) The setup function is Then you will need to call two member functions of this object to make gui work: (do not forget to set proper drawing mode):
YourObject.Draw(); YourObject.Animate();
And these three events for user interaction: OnMouseMoveEvent called with coordinates of the mouse above the window (0,0 at upper-left corner, positive x to the left, positive y to the bottom) but iKey is a pressed key, which takes values defined in keys.h (sjgui::CKeys), there are values that should be converted from whatever your function receives, to what is understand by the sjgui. By conversion I mean, when your function receives an event: left mouse button down, you should call the following code: You must convert following key values: SJ_KEY_MOUSE_LEFT, SJ_KEY_MOUSE_RIGHT,SJ_KEY_MOUSE_MIDDLE
Otherwise you will not be able to use your mouse, if you want sjgui::CEdit work properly you would need to convert following key codes: SJ_KEY_LEFT, SJ_KEY_UP, SJ_KEY_RIGHT, SJ_KEY_DOWN
SJ_KEY_PAGE_UP, SJ_KEY_PAGE_DOWN,
SJ_KEY_HOME, SJ_KEY_END, SJ_KEY_INSERT
Shift, alt and ctrl codes are not use by the GUI at the moment, but you also can convert them: SJ_KEY_SHIFT, SJ_KEY_ALT, SJ_KEY_CTRL
Helper class sjgui::CKeys is not used within existing controls yet, but it might be used in the feature, so I suggest you to call sjgui::OnKeyXXXEvent() functions, because they take care of functionality of this class.
- Let us look at a very simple example, let's say you need a label on the screen, what do you do? (I can assure you that you can put any other control instead of label) First create an object like Then you will put the following code inside of the initialization routine: And the last thing is to set things for drawing. I bet you have a function which draws everything at the end of this function you can include following lines:
SET_GL_FOR_GUI_DRAW(0,0,200,200);
my_label.Draw();
That is all! You should have your label displayed properly. Label does not receive any events and does not require animation that is why we avoided all other events.
- Now we can talk about: "What if I do not want to use glut and I want to use something else?". Well this question is answered above, you set whatever you want to use, and then just use controls as described, but if you want for your program to be absolutely portable, then I suggest you to wrap specific functions as described below: I interfaced your code with internals of the system by means of following functions:
- sjgui::Create(): which creates window.
- sjgui::GlobalMainLoop(): generates events.
- sjgui::IgnoreRepeates(): works with keyboard.
- sjgui::SwitchFullScreen(),sjgui::IsFullScreen(): controls fullscreen mode.
- sjgui::SetPos(),sjgui::SetScreenSize(): controls window geometry. How these functions works internally depends on implementation. I worked with glut all the time, because it is platform independent, but it has some drawbacks (like insufficient keyboard handling, lack of fullscreen mode). And I had goal to create gui, which would not relay on any particular library like glut. To test if I achieved my goal, I used code from http://nehe.gamedev.net (wonderful place to start learning OpenGL) to have win32 api windowing. Done with no problems, if you want to try, do following: define SJGUI_USE_WIN32_API as a preprocessor directive, recompile library and use this directive to compile any of the examples, I do not know how to tell the difference, between glut and win32 implementations, but it is working, even though not fully supported yet, like if you call sjgui::SetPos(), it will not move window to a proper position. I just did not have time to go read help on windows api :).
- Anyway the point is: if you would like to have different windowing system it can be done, very easily. Look at source code globals.cpp for more information, or post a message and it will be done :) meanwhile you can use it as it is keeping in mind, that, when your request is implemented, there will be almost nothing to change in your program, only function main (maybe even such thing would not be required).
- So what you do you is you build library with defined directive SJGUI_NO_WRAPPERS. and then you should provide implementations of functions described above, this is an example, in you main cpp you would need:
#define SJGUI_NO_WRAPPERS
#include <sjgui/sjgui.h>
namespace sjgui{
CWnd* pMainWindow=NULL;
int Create(const char* pcCaption,CWnd* pWnd,int iBits,bool yFullScreen)
{
assert(pWnd!=NULL);
pMainWindow=pWnd;
return OnInitEvent(pWnd);
}
void SetPos(int iX,int iY)
{
}
void SetScreenSize(int iW,int iH)
{
}
void GlobalMainLoop()
{
bool yRuning=true;
printf("I am running!\n");
while(yRuning)
{
}
}
void IgnoreRepeates(bool b)
{
}
void SwitchFullScreen(int iW,int iH,int iBits)
{
}
bool IsFullScreen()
{
return false;
}
}
That is all I have to say right now, hope it does make sense.
- Back to Lesson 7.
- This is full source code:
#define SJGUI_NO_WRAPPERS
#include <sjgui/sjgui.h>
namespace sjgui{
CWnd* pMainWindow=NULL;
int Create(const char* pcCaption,CWnd* pWnd,int iBits,bool yFullScreen)
{
assert(pWnd!=NULL);
pMainWindow=pWnd;
return OnInitEvent(pWnd);
}
void SetPos(int iX,int iY)
{
}
void SetScreenSize(int iW,int iH)
{
}
void GlobalMainLoop()
{
bool yRuning=true;
printf("I am running!\n");
while(yRuning)
{
}
}
void IgnoreRepeates(bool b)
{
}
void SwitchFullScreen(int iW,int iH,int iBits)
{
}
bool IsFullScreen()
{
return false;
}
}
class CSpcWnd : public sjgui::CWnd
{
};
int main(int argc, char* argv[])
{
CSpcWnd SpcWnd;
SpcWnd.PosWnd(20,50,320,240);
if(sjgui::Create(argv[0],&SpcWnd))
{
sjgui::GlobalMainLoop();
}
else
printf("Could not create opengl window.\n");
return 0;
}
|
|