Recent Changes - Search:

Documentation

Help

Tutorials

RoadMap

Help Docs for wiki writers:

PmWiki

edit SideBar

LessonTwo

Lesson Two:Advance Hello World: events, textboxes and graceful exits

In lesson one we learned how two open a new window and display text on the screen. This lesson we are going to learn more about textboxes and how to use events to gracefully exit our program.

we are going to start with some code that you are already familiar with.

#include "EpeeEngine.h"

int main (int argc, char *argv[])
{
EpeeEngine GraphicsEngine;
GraphicsEngine.SetUp(800,600,false,"Hello World",false,300,32);

now we are going to change the CreateTextBox? call by adding more arguments

GraphicsEngine.CreateTextBox("hello",20,50,3,"Hello World","Current",-1,-1,"ARIAL.TTF",18,255,0,255,"C:\\WINDOWS\\Fonts\\");

The argument "ARIAL.TTF" and "C:\\WINDOWS\\Fonts\\" are Windows specific so if you are using a different Operating System change them to fit your needs. To review "ARIAL.TTF" is the file name of the font you wish to use and "C:\\WINDOWS\\Fonts\\" is the path where your font is located. The 18 after "ARIAL.TTF" is the font size in this case we are going to us 18 font point. The next three arguments are the color of the text in the following order red, blue ,and green. So, in this case the text will be yellow.

next we are going to make the Textbox editable so we can change it. We add the following lines.

if(GraphicsEngine.FindTextBox("hello"))
{

This line checks to make sure that the text box hello exist. The Find functions can impact performance if the render list you are searching is really long. However the fact that the Find functions cache the last sprocket found means sequential call searching for the same sprocket in the same render list to not impact performance.

GraphicsEngine.FindTextBox("hello")->SetEditable(true); 

This line make the TextBox? editable so we can change it Since SetEditable? is part of the text box class we need to use FindTextBox? to return a pointer to the instance of the text box hello.

GraphicsEngine.FindTextBox("hello")->SetColorChangedOnClick(true);
                                       }

Now we tell the text box we want the background color to change when the textbox is is selected, and end our if statement.

SDL_Event event;

We create a SDL_Event for our event handling

while (1)
{

We start our game loop

while(GraphicsEngine.GetEvent(&event))
{

We now look for events before we start to render the seen The GetEvent? funtion fills and event object and handle text box keyboard events, and button clicking.

switch(event.type)
                    {
                      case SDL_QUIT:
                        return 0;
                        break;
                       case  SDL_KEYUP:
                           switch(event.key.keysym.sym)
                               {
                                     case SDLK_ESCAPE:
                                      return 0;
                                      break;
                                  }

                         break;
                     }    

These lines check for an escape key press and return zero to the main function to gracefully exit the program. The Epee Engine handles all the clean up for you. The case SDL_QUIT returns 0 to main function to end the program if the user close the window.

  }

GraphicsEngine.RenderSeen(true);

We now end our event polling loop and render the seen.

{
return 0;
}

Lastly we end our rendering loop and have a default return even though the program will never hit the return here

That is it here is the code with out comments

#include "EpeeEngine.h"

int main (int argc, char *argv[])
{
EpeeEngine GraphicsEngine;
GraphicsEngine.SetUp(800,600,false,"Hello World",false,300,32);
GraphicsEngine.CreateTextBox("hello",20,50,3,"Hello World","Current",-1,-1,"ARIAL.TTF",18,255,0,255,"C:\\WINDOWS\\Fonts\\");
if(GraphicsEngine.FindTextBox("hello"))
{
   GraphicsEngine.FindTextBox("hello")->SetEditable(true);                                    GraphicsEngine.FindTextBox("hello")->SetColorChangedOnClick(true);
 }
SDL_Event event;
while (1)
{
      while(GraphicsEngine.GetEvent(&event))
      {
         switch(event.type)
          {
              case SDL_QUIT:
                        return 0;
               break;
             case  SDL_KEYUP:
               switch(event.key.keysym.sym)
                 {
                       case SDLK_ESCAPE:
                          return 0;
                         break;
                   }

               break;
            }                      
       }

GraphicsEngine.RenderSeen(true);

}
return 0;
} 

You should see the a new window with the words hello world in the window. If you click on the words they should highlight blue. You can then delete the words and change to what ever you want. Note how you can only fit the same word length as hello world. In the next lesson we will look more closely at what that is and how you address that problem.

Like always if you have problems check the stdout.txt and stderr.txt files first and if you still have problems PM or e-mail me on the forum I would be happy to answer any questions

Edit - History - Print - Recent Changes - Search
Page last modified on March 12, 2008, at 02:17 PM