static void draw_text(void) { glPushAttrib(GL_ALL_ATTRIB_BITS); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0.f, viewport_width, viewport_height, 0.f, -1.f, 1.f); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor3f(1, 1, 1); draw_score_and_ships(); draw_level_diagram(); switch (inner_state.state) { case IS_WAVE_TITLE: draw_wave_title(); draw_powerup_bars(); break; case IS_IN_GAME: draw_multiplier_message(); draw_combo_message(); draw_powerup_bars(); break; case IS_GAME_OVER: case IS_RANK: draw_game_over(); break; case IS_WAVE_CLEARED: case IS_PRE_WAVE_CLEARED: draw_wave_cleared(); break; case IS_LEVEL_CLEARED: case IS_LEVEL_TRANSITION: draw_level_cleared(); break; } if (serializing) draw_serializing(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopAttrib(); }
void draw(Game_Data *data) { switch(current_state) { case GAME: draw_game(data); break; case TITLE: draw_title(data); break; case MENU: draw_menu(data); break; case GAME_OVER: draw_game_over(data); break; case OUT_OF_MEMORY: draw_oom(data); break; } }
//Main program int main() { /* Initialize SDL’s video system and check for errors */ if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Make sure SDL_Quit gets called when the program exits! */ atexit(SDL_Quit); /*set window title*/ SDL_WM_SetCaption("Essi Invaders", "P"); /* Attempt to set a 800x600 8 bit color video mode */ screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 8, SDL_DOUBLEBUF ); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //load images load_image("titlescreen.bmp", &title_screen, magenta); load_image("cmap.bmp", &cmap, magenta); load_image("target1.bmp", &invaders_img[0], magenta); load_image("target2.bmp", &invaders_img[1], magenta); load_image("target3.bmp", &invaders_img[2], magenta); load_image("target4.bmp", &invaders_img[3], magenta); load_image("target5.bmp", &invaders_img[4], magenta); load_image("target6.bmp", &invaders_img[5], magenta); load_image("target7.bmp", &invaders_img[6], magenta); load_image("target8.bmp", &invaders_img[7], magenta); load_image("target9.bmp", &invaders_img[8], magenta); load_image("target10.bmp", &invaders_img[9], magenta); load_image("target11.bmp", &invaders_img[10], magenta); load_image("target12.bmp", &invaders_img[11], magenta); load_image("spaceship.bmp", &player_img, magenta); load_image("essi.bmp", &logo_img, magenta); load_image("saucer.bmp", &saucer_img, magenta); load_image("gameover.bmp", &game_over_img, magenta); load_image("damage.bmp", &damage_img, lime); load_image("damagetop.bmp", &damage_top_img, lime); Uint32 next_game_tick = SDL_GetTicks(); int sleep = 0; Uint8 *keystate = 0; int quit = 0; SDL_Event event; init_score(); init_bases(); init_misil(); init_invaders(); init_player(); init_saucer(); init_bullets(bullets, P_BULLETS); init_bullets(enemy_bullets, E_BULLETS); state = menu; title_time = SDL_GetTicks(); /* Animation */ while (quit == 0) { /* Grab a snapshot of the keyboard. */ keystate = SDL_GetKeyState(NULL); while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch( event.key.keysym.sym ) { //exit out of game loop if escape is pressed case SDLK_ESCAPE: quit = 1; break; case SDLK_SPACE: if (state == menu) { state = game; } else if (state == game){ player_shoot(); saucer_ai(); } else if (state == game_over) { init_invaders(); init_bases(); init_score(); init_player(); state = game; match = -1; } break; default: break; } break; case SDL_KEYUP: switch( event.key.keysym.sym ) { case SDLK_UP: move_misil(up); break; case SDLK_DOWN: move_misil(down); break; default: break; } } } draw_background(); if (state == menu) { char s[] = "Press SPACEBAR to start"; SDL_Rect src[60]; int i; if (title_time + 2000 < SDL_GetTicks()) { src[0].x = (SCREEN_WIDTH/100)*30; //Yellow Background of Space Invaders src[0].y = 40; src[0].w = 440; src[0].h = 230; SDL_FillRect(screen, &src[0], 248); } else { int y = 0; for (i = 0; i < 60; i++) { src[i].x = 0; src[i].y = y; src[i].w = SCREEN_WIDTH; src[i].h = 10; SDL_FillRect(screen, &src[i], 227); y += 10; } for (i = 0; i < 60; i++) { SDL_FillRect(screen, &src[i], rand() % 255); } } draw_title_screen(); draw_string(s, (SCREEN_WIDTH / 2) - (strlen(s) * 10), 500); } else if (state == game) { //move player if (keystate[SDLK_LEFT]) { move_player(left); } if (keystate[SDLK_RIGHT]) { move_player(right); } draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_serviceProjectMatch(match); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); enemy_hit_collision(); player_hit_collision(); enemy_base_collision(); saucer_hit_collision(); bullet_base_collision(enemy_bullets, E_BULLETS, 1); bullet_base_collision(bullets, P_BULLETS, 0); enemy_player_collision(); move_invaders(invaders.speed); move_saucer(); movenemy_bullets(bullets, P_BULLETS, -30); movenemy_bullets(enemy_bullets, E_BULLETS, 15); calculate_level(); enemy_ai(); game_over_ai(); pause_game(); } else if (state == game_over) { draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); draw_game_over(); } else if (state == pause) { draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); pause_game(); } /* Ask SDL to update the entire screen. */ SDL_Flip(screen); next_game_tick += 1000 / 30; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } return 0; }
int main() { SDL_Surface *temp; /* Initialize SDL’s video system and check for errors */ if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Make sure SDL_Quit gets called when the program exits! */ atexit(SDL_Quit); /* Attempt to set a 640x480 8 bit color video mode */ screen = SDL_SetVideoMode(640, 480, 8,SDL_DOUBLEBUF); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("numbermap.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ Uint32 colorkey = SDL_MapRGB(temp->format, 255, 0, 255); SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen numbermap = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("title.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen title = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("gameover.bmp"); if (temp == NULL) { printf("Unable to load gameover.bmp.\n"); return 1; } //convert the end surface to the same type as the screen end = SDL_DisplayFormat(temp); if (end == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); /* Initialize the ball position data. */ init_ball(); int quit = 0; int state = 0; Uint8 *keystate = 0; Uint32 next_game_tick = SDL_GetTicks(); int sleep = 0; int r = 0; /* Animate */ while (quit == 0) { /* Update SDL's internal input state information. */ SDL_PumpEvents(); /* Grab a snapshot of the keyboard. */ keystate = SDL_GetKeyState(NULL); /* Respond to input. */ if (keystate[SDLK_ESCAPE]) { quit = 1; } if (keystate[SDLK_DOWN]) { move_paddle(0); } if (keystate[SDLK_UP]) { move_paddle(1); } //draw the background draw_background(); //display main menu if (state == 0 ) { if (keystate[SDLK_SPACE]) { state = 1; } //draw menu draw_menu(); //display gameover } else if (state == 2) { if (keystate[SDLK_SPACE]) { state = 0; //delay for a little bit so the space bar press dosnt get triggered twice //while the main menu is showing SDL_Delay(500); } if (r == 1) { //if player 1 is AI if player 1 was human display the return value of r not 3 draw_game_over(3); } else { //display gameover draw_game_over(r); } //display the game } else if (state == 1){ //check score r = check_score(); if (r == 1) { state = 2; } else if (r == 2){ state = 2; } //paddle ai movement move_paddle_ai(); /* Move the balls for the next frame. */ move_ball(); //draw net draw_net(); //draw paddles draw_paddle(); /* Put the ball on the screen. */ draw_ball(); //draw the score draw_player_0_score(); //draw the score draw_player_1_score(); } /* Ask SDL to update the entire screen. */ SDL_Flip(screen); next_game_tick += 1000 / 60; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } return 0; }