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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#include <stdlib.h>
#include "SDL.h"
#include "SDL_ttf.h"
// 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][30] = {"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=-1;
int max_index=3;
//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_FreeSurface(fontSurface);
// 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 Print_Question()
{
static int bgcolor = 0x77 ;
// clear screen
drawrect(0,0,640,480,bgcolor);
//imgui_prepare();
//char *q=char(question[index]);
printF(question[index], 100, 100);
//printF(q, 100, 100);
//printF(answer[index], 60, 190);
//printF("Your question", 100, 100);
if (button(1,50,180)) {
score =score + 10;
index++;
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);
//Check index
//button(6,200,350);
//printF(itoa(question[index][0],buffer,10), 360, 390);
//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[])
{
//The images
SDL_Surface* bgimg = NULL;
SDL_Surface* screen = NULL;
//Load Image
bgimg = SDL_LoadBMP( "hello.bmp" );
//Apply to Screen
SDL_BlitSurface( bgimg, NULL, screen, NULL );
//Update Screen
SDL_Flip( screen );
// 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);
}
if (button(2,350,180)){
Print_Question();
}
printF("Enter The Quiz!", 360, 190);
// printF("Welcome to this game!",60,200);
// printF("Select an Option Below!",70,200);
//if (button(3,70,210)){
// Main loop: loop forever.
index++;
while (index<=max_index)
{
// Render stuff
//index++;
//if (index <=50)
//Show_Main_Menu();
//static int bgcolor = 0x77 ;
//drawrect(0,0,640,480,bgcolor);
Print_Question();
//Print_Question();
//}
// 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;
}
|