SDL game state

Hi,

I am a game state main logic (post 1) and I have a button draw function render (post 2). How can I add the render function to the game state logic so that when I can show the question and anwser button after pressing "g" to start game?


Post 1==> Game State Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
struct StateStruct 
{
	void (*StatePointer)();
};

stack<StateStruct> g_StateStack;     // Our state stack
SDL_Surface*       g_Bitmap = NULL;  // Our background image
SDL_Surface*       g_Window = NULL;  // Our backbuffer
SDL_Event		   g_Event;          // An SDL event structure for input
int				   g_Timer;          // Our timer is just an integer

void Menu();
void Game();
void Exit();

void DrawBackground();
void ClearScreen();
void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB);
void HandleMenuInput();
void HandleGameInput();
void HandleExitInput();

void Init();
void Shutdown();

int main(int argc, char **argv)
{
	Init();
	
	while (!g_StateStack.empty())
	{
		g_StateStack.top().StatePointer();		
	}

	Shutdown();

	return 0;
}

void Init()
{
	SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER);

	g_Window = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_ANYFORMAT);    

	SDL_WM_SetCaption(WINDOW_CAPTION, 0);

	g_Timer = SDL_GetTicks();

	g_Bitmap = SDL_LoadBMP("data/background.bmp");	   

	StateStruct state;
	state.StatePointer = Exit;
	g_StateStack.push(state);

	state.StatePointer = Menu;
	g_StateStack.push(state);

	TTF_Init();
}

void Shutdown()
{
	TTF_Quit();

	SDL_FreeSurface(g_Bitmap);
	SDL_FreeSurface(g_Window);

	SDL_Quit();
}


void Menu()
{

	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		HandleMenuInput();

		ClearScreen();

		DisplayText("Start (G)ame", 350, 250, 12, 255, 255, 255, 0, 0, 0);
		DisplayText("(Q)uit Game",  350, 270, 12, 255, 255, 255, 0, 0, 0);

		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		g_Timer = SDL_GetTicks();
	}	
}

void Game()
{	
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		HandleGameInput();

		ClearScreen();

		DrawBackground();

		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		g_Timer = SDL_GetTicks();
	}	
}


void Exit()
{	
	if ( (SDL_GetTicks() - g_Timer) >= FRAME_RATE )
	{
		HandleExitInput();

		ClearScreen();

		DisplayText("Quit Game (Y or N)?", 350, 250, 12, 255, 255, 255, 0, 0, 0);

		SDL_UpdateRect(g_Window, 0, 0, 0, 0);

		g_Timer = SDL_GetTicks();
	}	
}

void DrawBackground() 
{
	SDL_Rect source      = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };		
	SDL_Rect destination = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
	
	SDL_BlitSurface(g_Bitmap, &source, g_Window, &destination);
}

void ClearScreen()
{
	SDL_FillRect(g_Window, 0, 0);
}


void DisplayText(string text, int x, int y, int size, int fR, int fG, int fB, int bR, int bG, int bB) 
{

        TTF_Font* font = TTF_OpenFont("arial.ttf", size);

	SDL_Color foreground  = { fR, fG, fB};   // Text color. //
	SDL_Color background  = { bR, bG, bB };  // Color of what's behind the text. //

	SDL_Surface* temp = TTF_RenderText_Shaded(font, text.c_str(), foreground, background);

	SDL_Rect destination = { x, y, 0, 0 };
	
	SDL_BlitSurface(temp, NULL, g_Window, &destination);

	SDL_FreeSurface(temp);

	TTF_CloseFont(font);
}

void HandleMenuInput() 
{
	if ( SDL_PollEvent(&g_Event) )
	{
		if (g_Event.type == SDL_QUIT)
		{			
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  // game is over, exit the function
		}

		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				return;
			}

			if (g_Event.key.keysym.sym == SDLK_q)
			{
				g_StateStack.pop();
				return;
			}

			if (g_Event.key.keysym.sym == SDLK_g)
			{
				StateStruct temp;
				temp.StatePointer = Game;
				g_StateStack.push(temp);
				return; 
			}
		}
	}
}

void HandleGameInput() 
{
	if ( SDL_PollEvent(&g_Event) )
	{
		if (g_Event.type == SDL_QUIT)
		{			
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  // game is over, exit the function
		}

		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				
				return;  // this state is done, exit the function 
			}			
		}
	}
}

void HandleExitInput() 
{
	if ( SDL_PollEvent(&g_Event) )
	{
		if (g_Event.type == SDL_QUIT)
		{			
			while (!g_StateStack.empty())
			{
				g_StateStack.pop();
			}

			return;  
		}

		if (g_Event.type == SDL_KEYDOWN)
		{
			if (g_Event.key.keysym.sym == SDLK_ESCAPE)
			{
				g_StateStack.pop();
				
				return; 
			}

			if (g_Event.key.keysym.sym == SDLK_y)
			{
				g_StateStack.pop();
				return;
			}
			
			if (g_Event.key.keysym.sym == SDLK_n)
			{
				StateStruct temp;
				temp.StatePointer = Menu;
				g_StateStack.push(temp);
				return;
			}
		}
	}
}
Post 2 ==> render function (draw question and answer buttons)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Screen surface
SDL_Surface *gScreen;
SDL_Surface* fontSurface;
SDL_Color fColor;
SDL_Rect fontRect;
TTF_Font* font;
int score=50;
char buffer [50];
char question[4] = {'Your Question 1','Your Question 2','Your Question 3','Your Question 4'};
//char choices[4]= {'A. Option A','B. Option B','C. Option C','D. Option D'};
//char correct_ans[4] = {'B','C','D','A'};
int index=0;

//Initialize the font, set to white
void fontInit(){
        TTF_Init();
        font = TTF_OpenFont("arial.ttf", 28);
        fColor.r = 0;
        fColor.g = 0;
        fColor.b = 255;
}

//Print the designated string at the specified coordinates
void printF(char *c, int x, int y){
        fontSurface = TTF_RenderText_Solid(font, c, fColor);
        fontRect.x = x;
        fontRect.y = y;
        SDL_BlitSurface(fontSurface, NULL, gScreen, &fontRect);
//        SDL_Flip(gScreen);
}

struct UIState
{
  int mousex;
  int mousey;
  int mousedown;

  int hotitem;
  int activeitem;
} 
uistate = {0,0,0,0,0};

// Simplified interface to SDL's fillrect call
void drawrect(int x, int y, int w, int h, int color)
{
  SDL_Rect r;
  r.x = x;
  r.y = y;
  r.w = w;
  r.h = h;
  SDL_FillRect(gScreen, &r, color);
}

// Check whether current mouse position is within a rectangle
int regionhit(int x, int y, int w, int h)
{
  if (uistate.mousex < x ||
    uistate.mousey < y ||
    uistate.mousex >= x + w ||
    uistate.mousey >= y + h)
    return 0;
  return 1;
}

// Simple button IMGUI widget
int button(int id, int x, int y)
{
  // Check whether the button should be hot
  if (regionhit(x, y, 220, 48))
  {
    uistate.hotitem = id;
    if (uistate.activeitem == 0 && uistate.mousedown)
      uistate.activeitem = id;
  }

  // Render button 
  drawrect(x+8, y+8, 220, 48, 0);
  if (uistate.hotitem == id)
  {
    if (uistate.activeitem == id)
    {
      // Button is both 'hot' and 'active'
      drawrect(x+2, y+2, 220, 48, 0xffffff);
    }
    else
    {
      // Button is merely 'hot'
      drawrect(x, y, 220, 48, 0xffffff);
    }
  }
  else
  {
    // button is not hot, but it may be active    
    drawrect(x, y, 220, 48, 0xaaaaaa);
  }

  // If button is hot and active, but mouse button is not
  // down, the user must have clicked the button.
  if (uistate.mousedown == 0 && 
    uistate.hotitem == id && 
    uistate.activeitem == id)
    return 1;

  // Otherwise, no clicky.
  return 0;
}

// Prepare for IMGUI code
void imgui_prepare()
{
  uistate.hotitem = 0;
}

// Finish up after IMGUI code
void imgui_finish()
{
  if (uistate.mousedown == 0)
  {
    uistate.activeitem = 0;
  }
  else
  {
    if (uistate.activeitem == 0)
      uistate.activeitem = -1;
  }
}

// Rendering function
void render()
{ 
  static int bgcolor = 0x77 ;

  // clear screen
  drawrect(0,0,640,480,bgcolor);

  imgui_prepare();
  
  //char *q=char(question[index]);
  //printF(question[0], 100, 100);
  //printF(q, 100, 100);
 
  //printF(answer[index], 60, 190);
     
  //printF("Your question", 100, 100);
    
  if (button(1,50,180)) {
	  score =score + 10;
	  exit;
  }
  printF("A.Your Answer 1", 60, 190);
  
  if (button(2,350,180)){
	  score=score - 10;
  }
  printF("B.Your Answer 2", 360, 190);
  
  if (button(3,50,280)){
	  score=score - 10;
  }
  printF("C.Your Answer 3", 60, 290);
    
  if (button(4,350,280)){
	  score=score - 10;
  }
  printF("D.Your Answer 4", 360, 290);
  
  //Score Button
  button(5,100,350);
  //convert the score to char and use base 10 to display
  printF(itoa(score,buffer,10), 110, 360);
  
	  
  //if (button(3,50,250))
  //  bgcolor = (SDL_GetTicks() * 0xc0cac01a) | 0x77;
  
  //if (button(5,100,400)){
  //  exit(0);
  //}
  
  imgui_finish();

  // update the screen
    SDL_UpdateRect(gScreen, 0, 0, 640, 480);    

  // don't take all the cpu time
  SDL_Delay(10); 
  
}


// Entry point
int main(int argc, char *argv[])
{
  // Initialize SDL's subsystems - in this case, only video.
    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
  {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
    }

  // Register SDL_Quit to be called at exit; makes sure things are
  // cleaned up when we quit.
    atexit(SDL_Quit);
    
  // Attempt to create a 640x480 window with 32bit pixels.
    gScreen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  
  //Initialize fonts
    fontInit();

  // If we fail, return error.
    if (gScreen == NULL) 
  {
        fprintf(stderr, "Unable to set up video: %s\n", SDL_GetError());
        exit(1);
    }

	
  // Main loop: loop forever.
	//index=0;
    while (1)
    {
    // Render stuff
		
		render();
					
    // Poll for events, and handle the ones we care about.
        SDL_Event event;
        while (SDL_PollEvent(&event)) 
        {
            switch (event.type) 
            {
      case SDL_MOUSEMOTION:
        // update mouse position
        uistate.mousex = event.motion.x;
        uistate.mousey = event.motion.y;
        break;
      case SDL_MOUSEBUTTONDOWN:
        // update button down state if left-clicking
        if (event.button.button == 1)
          uistate.mousedown = 1;
        break;
      case SDL_MOUSEBUTTONUP:
        // update button down state if left-clicking
        if (event.button.button == 1)
          uistate.mousedown = 0;
        break;
            case SDL_KEYUP:                  
        switch (event.key.keysym.sym)
        {
        case SDLK_ESCAPE:
          // If escape is pressed, return (and thus, quit)
          return 0;
        }
                break;
            case SDL_QUIT:
                return(0);
            }
        }
		
    }
    return 0;
}
Anyone please help?
Please help, I need it urgently.
thousand thanks~~~~
Topic archived. No new replies allowed.