Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Lesson 8

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
 // Right now initializes fonts for all controls
 // but in feature something else could be added inside
 sjgui::OnInitEvent(&YourObject); 
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:
 sjgui::OnMouseMoveEvent(&YourObject,iX,iY);
 sjgui::OnKeyUpEvent(&YourObject,iKey);
 sjgui::OnKeyDownEvent(&YourObject,iKey);
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:
 sjgui::OnKeyDownEvent(&YourObject,SJ_KEY_MOUSE_LEFT);
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
 #include <sjgui/sjgui.h>
 // put label on the screen, will have default text Label.
 sjgui::CLabel my_label;
Then you will put the following code inside of the initialization routine:
 // font initialization
 sjgui::OnInitEvent(my_label);
 // top-left corner of the screen
 my_label.PosWnd(0,0,300,20);
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 size and position of the viewport which will be used for gui
 SET_GL_FOR_GUI_DRAW(0,0,200,200);
 my_label.Draw(); // Draw the control!
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:
// For this lesson you will learn how to connect sjgui with the system calls.

// We should switch off built in wrappers, because we are creating our own.
#define SJGUI_NO_WRAPPERS

// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

namespace sjgui{ // these functions should belong to the sjgui namespace

// Global object
CWnd* pMainWindow=NULL;
// Creates your window
int Create(const char* pcCaption,CWnd* pWnd,int iBits,bool yFullScreen)
{
    assert(pWnd!=NULL); // just a check
    // note that pWnd must be stored and used as a receiver of events
    // your code here
    pMainWindow=pWnd; // remember for usage in other functions
    return OnInitEvent(pWnd);
}
// Set position of the main window.
void SetPos(int iX,int iY)
{
    // your code here
}
//Set size of the window.
void SetScreenSize(int iW,int iH)
{
    // your code here
}
// Main loop of the program.
void GlobalMainLoop()
{
    bool yRuning=true;
    // just to let you know.
    printf("I am running!\n");
    while(yRuning)
    {
        // your code here
        // somehow you should call
        // events OnKeyDownEvent(pMainWindow,x,x), 
        // OnKeyUpEvent(pMainWindow,x,x), 
        // OnMouseMoveEvent(pMainWindow,x,x);
    }
}
// On/off repeating of not release key.
void IgnoreRepeates(bool b)
{
    // your code here
}

//Switch program into a fullscreen/window mode.
void SwitchFullScreen(int iW,int iH,int iBits)
{
    // your code here
}
// Returns true if program is in a full screen.
bool IsFullScreen()
{
    // your code here
    // you should return the correct mode
    return false;
}

} // end of namespace sjgui
That is all I have to say right now, hope it does make sense.
Back to Lesson 7.
This is full source code:
// For this lesson you will learn how to connect sjgui with the system calls.

// We should switch off built in wrappers, because we are creating our own.
#define SJGUI_NO_WRAPPERS

// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

namespace sjgui{ // these functions should belong to the sjgui namespace

// Global object
CWnd* pMainWindow=NULL;
// Creates your window
int Create(const char* pcCaption,CWnd* pWnd,int iBits,bool yFullScreen)
{
    assert(pWnd!=NULL); // just a check
    // note that pWnd must be stored and used as a receiver of events
    // your code here
    pMainWindow=pWnd; // remember for usage in other functions
    return OnInitEvent(pWnd);
}
// Set position of the main window.
void SetPos(int iX,int iY)
{
    // your code here
}
//Set size of the window.
void SetScreenSize(int iW,int iH)
{
    // your code here
}
// Main loop of the program.
void GlobalMainLoop()
{
    bool yRuning=true;
    // just to let you know.
    printf("I am running!\n");
    while(yRuning)
    {
        // your code here
        // somehow you should call
        // events OnKeyDownEvent(pMainWindow,x,x), 
        // OnKeyUpEvent(pMainWindow,x,x), 
        // OnMouseMoveEvent(pMainWindow,x,x);
    }
}
// On/off repeating of not release key.
void IgnoreRepeates(bool b)
{
    // your code here
}

//Switch program into a fullscreen/window mode.
void SwitchFullScreen(int iW,int iH,int iBits)
{
    // your code here
}
// Returns true if program is in a full screen.
bool IsFullScreen()
{
    // your code here
    // you should return the correct mode
    return false;
}

} // end of namespace sjgui

// This class is used to process all events. But for this lesson, 
// we do not want to process any events, so let's leave it blank.
class CSpcWnd : public sjgui::CWnd
{
}; // end of CSpcWnd class

// This is the way you create window and let it be running.
// Once this is done, all modifications required by sjgui, 
// could be done within space window class CSpcWnd
int main(int argc, char* argv[])
{
    // first thing we should do, is to create 
    // an instance of our space window.
    CSpcWnd SpcWnd;
    // Position the window,
    SpcWnd.PosWnd(20,50,320,240);
    // Then we create a window which will be assigned to our
    // space window class.
    // Given parameters are the name of the window
    // (I am using name of the program, which is stored in arv[0])
    // and the class, which will receive all events.
    // Function reruns non zero value in case of the success
    if(sjgui::Create(argv[0],&SpcWnd))
    {
        // Run it. Now all events are transferred to our class.
        // And when we are done, program will be finished.
        sjgui::GlobalMainLoop();
    }
    else
        // Print error message if something was wrong.
        printf("Could not create opengl window.\n");
    return 0;
} // the end of main
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:05 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo