|
This is not an article about some new ideas or some new technology, it's
just a little article about making message boxes and buttons in SDL.
Hope it will be useful for somebody.
Imagine that you want to output
some kind of information during the game and receive some kind of answer
from the player, or just make a menu with all this options like "new
game", "save game", "quit", etc. Yes, you need to use message boxes and
buttons. That's what I will explain here in this article.
First of all, message boxes can
be implemented for all kind of purposes: menu messages, pause messages,
some state messages, etc all these can be created
using message boxes, and of course buttons |
|
|
on
them. If you are familiar with OOP, maybe you have already
understood how we will design all these stuff: there will be 2 clases:
Message
class - it will create and draw a square with dimensions,
color, etc passed as parameters, print some text string, and may
or may not include any number of buttons
or none. Button class - will
create a button. It will store button's x and y position and SDL_Surface
pointer to button sprite.
Well. lets take a look at cMessage.h:
|
#ifndef _CMESSAGE_H
#define _CMESSAGE_H
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <stdlib.h>
#include <vector>
#include "cButton.h"
using namespace std;
class cMessage
{
private:
Uint32 x, y; //x and y coordinates of
left upper msg vertex
Uint32 w, h; //width and height of
msg
Uint32 r; //red mask
Uint32 g; //green mask
Uint32 b; //blue mask
SDL_Surface *surf_msg; //surface where msg
with text will be on
vector <cButton*> btn; //vector of pointers
to cButton structures
public:
cMessage(Uint32 x, Uint32 y, Uint32 w,
Uint32 h, Uint32 r, Uint32 g, Uint32 b,
char *msg,
Uint8 r8, Uint8 g8, Uint8 b8, vector
<cButton*> btn);
~cMessage();
void SetPosition(Uint32 x, Uint32 y);
//Changes current position
Uint32 GetMouseClick(); //Get button
id of clicked button
//0 if no button were clicked
void Render(SDL_Surface* surf_to);
//Draw msg box
};
#endif /*
_CMESSAGE_H */ |
As you can see there are several object properties,
that will be initialized when object is created (they are passed as
parameters, take a look at the constructor), some of these properties
will be used only once in code like r, g, b values, but it's better to
store them if we want to change its values in some part of program (we
need to create new function that will do that). The last constructor's
parameter can be NULL if you don't want to put any button on msg box. One
of functions of interest is
Uint32 GetMouseClick() it returns button's
ID by checking what button was clicked, if no button was clicked it
returns 0. Read cMessage.cpp for more info.
This is the code for
its usage:
//.......
cMessage msg1(100, 50, 200, 300, 10, 100, 10,
"Menu", 0xAA, 0xBB, 0xAA, NULL); msg1.Render(screen); SDL_Delay(10000);
//........ |
|