int init() { srand(time(0) ^ getpid()); if (SDL_Init(0) < 0) { fprintf(stderr, "Error initialising SDL: %s\n", SDL_GetError()); return -1; } if (config_init() == -1 || input_init() == -1 || graphics_init() == -1 || sound_init() == -1 || level_init() == -1 || bomber_init() == -1 || bomb_init() == -1) { return -1; } // load images/animations - its done up ^ in init's // init calc_delta_time calc_delta_time(); return 0; }
void state_init (struct state *s) { player_init (s); s->mode = mode_move; s->thr.id_item = ID_NO_ITEM; s->thr.x = 1; s->thr.y = 1; s->status.score = 0; s->status.level = 1; int i; for(i=0; i<EFFECTS_NUM; ++i){ s->status.effect[i] = 0; } level_init (s); }
const struct Allocator* SLAB_Init(uint8 shifts, const uint32 config_array[]) { #ifndef __amigaos4__ struct SLAB_Allocator *sa = (struct SLAB_Allocator*)AllocVec(sizeof(struct SLAB_Allocator), MEMF_ANY | MEMF_CLEAR); #else struct SLAB_Allocator *sa = (struct SLAB_Allocator*)IExec->AllocVec(sizeof(struct SLAB_Allocator), MEMF_ANY | MEMF_CLEAR); #endif sa->allocator.alloc = &SLAB_Alloc; sa->allocator.free = &SLAB_Free; sa->first_shift = SLAB_FIRST_SHIFT; sa->last_shift = SLAB_FIRST_SHIFT + shifts; #ifndef __amigaos4__ sa->levels = (struct SLAB_Level*)AllocVec((shifts + 1) * sizeof(struct SLAB_Level), MEMF_ANY|MEMF_CLEAR); #else sa->levels = (struct SLAB_Level*)IExec->AllocVec((shifts + 1) * sizeof(struct SLAB_Level), MEMF_ANY|MEMF_CLEAR); #endif if(sa->levels) { uint8 x; for(x = 0; x < shifts; ++x) { if(!level_init(&sa->levels[x], sa->first_shift + x, config_array[x])) { SLAB_Cleanup(&sa->allocator); sa = 0; break; } } } else { SLAB_Cleanup(&sa->allocator); sa = 0; } return &sa->allocator; }
int main( int argc, char* args[] ) { //Quit flag bool quit = false; Sprite mySprite(40, 40, 0, 40, 40); //The letter Letter myLetter; //The letterspace Letterspace myLetterspace( 600, 50); //The frame rate regulator Timer fps; //Initialize if( init() == false ) { return 1; } //Load the files if ( level_init() == false ) { return 1; } //While the user hasn't quit while( quit == false ) { //Start the frame timer fps.start(); //While there's events to handle while( SDL_PollEvent( &event ) ) { //Handle events for the letter myLetter.handle_input(); mySprite.input(); //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //Move the letter myLetter.move(); mySprite.move(); //Fill the screen white SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) ); level_files(); //Show the letterspace myLetterspace.show(); myLetter.init("../media/a.png"); // mySprite.init(); //Show the letter on the screen myLetter.show(); mySprite.show(); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //Cap the frame rate if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } //End the level level_end(); //mySprite.~Sprite(); //Clean up clean_up(); return 0; }
game_status state_in_game(SDL_Renderer* renderer, game* p_game) { bool done=false; //the return code is always GAME_OVER, the player try to beat his hi-score //until his demise game_status ret_code = GAME_OVER; long timelastcall=SDL_GetTicks(); //initialize the states of completed lines to 0 memset(p_game->state, 0, STATE_NUM * sizeof(int)); p_game->score = 0; int level_cur = 0; int level_max = 10; level_init(); //declare two shapes, one controled by the player, the other to indicates //the type of the next shape shape o_falling_shape, o_preview_shape; //set the falling shape type randomly shape_set(&o_falling_shape, rand() % SHAPE_NB); shape_default_coord(&o_falling_shape); shape_type next_shape_type = rand() % SHAPE_NB; shape_set(&o_preview_shape, next_shape_type); //put the preview shape on the side of the board o_preview_shape.x = LEVEL_WIDTH + 1; o_preview_shape.y = o_preview_shape.len - SHAPE_SIZE / 2 + 2; while (!done){ //drawing SDL_SetRenderDrawColor(renderer, 0, 42, 0, 255); SDL_RenderClear(renderer); //draw the falling shape shape_draw(&o_falling_shape, p_game->p_fontmap, renderer); //draw the next shape as a preview shape_draw(&o_preview_shape, p_game->p_fontmap, renderer); //draw the level (empty blocks and alderly placed shapes) level_draw(renderer, &o_falling_shape, p_game->p_fontmap); int game_speed = level_max * 100 - level_cur * 100; int total=0; for(int i=0;i<3;i++)total += p_game->state[i]; //draw a line between the board and the score / next shape area SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0xFF, 0xFF ); SDL_RenderDrawLine(renderer, TILE_SIZE * 12, 0, TILE_SIZE * 12, SCREEN_HEIGHT); //draw the score fontmap_printf(p_game->p_fontmap, SCREEN_WIDTH - 7*5, SCREEN_HEIGHT - 120, renderer, "%d", p_game->score); SDL_Event event; while (SDL_PollEvent(&event)){ if((event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN)|| (event.type == SDL_JOYBUTTONDOWN && event.jbutton.button == 5)){ ret_code = state_paused(renderer); if(ret_code!=STAY){ done = 1; } } if(event.type == SDL_QUIT){ exit(EXIT_SUCCESS); } switch ( event.type ) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_RIGHT: if(shape_move(&o_falling_shape, 1, 0))o_falling_shape.x++; break; case SDLK_LEFT: if(shape_move(&o_falling_shape, -1, 0))o_falling_shape.x--; break; case SDLK_DOWN: if(shape_move(&o_falling_shape, 0, 1)) timelastcall=SDL_GetTicks()+game_speed; break; case SDLK_SPACE: while(shape_move(&o_falling_shape, 0, 1)) o_falling_shape.y++; timelastcall=SDL_GetTicks()+game_speed; break; case SDLK_f: shape_rotate(&o_falling_shape, 0); break; case SDLK_d: shape_rotate(&o_falling_shape, 1); break; case SDLK_q: exit(EXIT_SUCCESS); break; default: break; } break; case SDL_JOYBUTTONDOWN: switch(event.jbutton.button) { case 0: while(shape_move(&o_falling_shape, 0, 1)) o_falling_shape.y++; timelastcall=SDL_GetTicks()+game_speed; break; case 2: shape_rotate(&o_falling_shape, 1); break; case 3: shape_rotate(&o_falling_shape, 0); break; case 6: done=true; ret_code = QUIT; break; case 19: done=true; ret_code = QUIT; break; default: break; } break; } } if(SDL_GetTicks() - timelastcall > game_speed) { //check if the falling shape can move if(shape_move(&o_falling_shape, 0, 1)){ //update coord o_falling_shape.y++; }else{ //the shape can not move int i; level_add_shape(&o_falling_shape); int lines_in_a_row=0; //tab of line index to remove int rem_tab[SHAPE_SIZE]; int line_nb = level_check_line(rem_tab); for(i=1;i<line_nb;i++){ if(rem_tab[i] == rem_tab[i-1]+1)lines_in_a_row++; else lines_in_a_row = 0; } if(line_nb){ //blink completed line(s) blink(renderer, rem_tab, line_nb, p_game->p_fontmap); for(i=0;i<line_nb;i++)level_remove_line(rem_tab[i]); //remove completed line(s) p_game->score += line_nb * (lines_in_a_row + 1); //update score p_game->state[lines_in_a_row]++; //update game states if(line_nb == 2 && lines_in_a_row == 0)p_game->state[0]++; level_cur = p_game->score / 1000; //eventually update level number if(level_cur >= level_max)level_cur = level_max; } if(level_check_game_over()){ done = 1; ret_code = GAME_OVER; } else{ //"create" a new falling block by chaning the falling shape type //to the next shape and reseting the coords shape_set(&o_falling_shape, next_shape_type); shape_default_coord(&o_falling_shape); //pick a next shape type randomly for the preview and the next falling //block next_shape_type = rand() % SHAPE_NB; shape_set(&o_preview_shape, next_shape_type); } } //update timer timelastcall=SDL_GetTicks(); } SDL_RenderPresent(renderer); } return ret_code; }
int main(int argc, char *argv[]) { int k; struct coord c; unsigned int i; unsigned char turn_taken = 1, running = 0; seed_rng(); display_init(); if(player_init() == -1) return 0; level_init(&levels[0], 8); player_set_level(&levels[0]); while(1) { main_clear(); player_see(); player_status(); turn_taken = 0; if(!running) k = display_getch(); msg_clear(); switch(k) { case '8': case '9': case '6': case '3': case '2': case '1': case '4': case '7': case KEY_UP: case KEY_DOWN: case KEY_LEFT: case KEY_RIGHT: c = key_to_direction(k); if(!player_move(c.x, c.y)) running = 0; turn_taken = 1; break; case 'c': c = key_to_direction(ask_key("In which direction?")); msg_clear(); player_close(c.x, c.y); turn_taken = 1; break; case 'o': c = key_to_direction(ask_key("In which direction?")); msg_clear(); player_open(c.x, c.y); turn_taken = 1; break; case 'k': c = key_to_direction(ask_key("In which direction?")); msg_clear(); player_kick(c.x, c.y); turn_taken = 1; break; case 'g': case 'G': k = ask_key("In which direction?"); c = key_to_direction(k); if((c.x || c.y) && player_move(c.x, c.y)) running = 1; turn_taken = 1; break; case ':': player_look(); break; case ';': msg_printf("Pick an object..."); c = player_select_square(); msg_clear(); player_remote_look(c.x, c.y); break; case '.': turn_taken = 1; break; } if(turn_taken) { if(player_poll()) running = 0; for(i = 0; i < levels[0].nmonst; i++) if(monster_poll(levels[0].monsters[i])) running = 0; } } return 0; }
static bool init(void) { if(!al_init()) { fprintf(stderr, "failed to initialize allegro.\n"); return false; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize keyboard.\n"); return false; } if(!al_init_image_addon()) { fprintf(stderr, "failed to initialize image system.\n"); return false; } al_init_font_addon(); if(!al_init_ttf_addon()) { fprintf(stderr, "failed to initialize ttf system.\n"); return false; } /* sound */ if(!al_install_audio()) { fprintf(stderr, "failed to initialize audio system.\n"); return false; } if(!al_init_acodec_addon()) { fprintf(stderr, "failed to initialize audio codecs.\n"); return false; } if(!al_reserve_samples(10)) { fprintf(stderr, "failed to reserve audio samples.\n"); return false; } /* fonts */ asteroids.small_font = al_load_ttf_font("data/vectorb.ttf", 12, 0); asteroids.large_font = al_load_ttf_font("data/vectorb.ttf", 24, 0); /* lives sprite */ asteroids.lives_sprite = al_load_bitmap("data/sprites/ship/ship.png"); if(!asteroids.lives_sprite) { fprintf(stderr, "failed to load lives sprite.\n"); return false; } /* sprite preloading */ if(!level_init()) return false; if(!ship_init()) return false; if(!missile_init()) return false; if(!saucer_init()) return false; if(!asteroid_init()) return false; if(!explosion_init()) return false; asteroids.timer = al_create_timer(1.0 / FPS); if(!asteroids.timer) { fprintf(stderr, "failed to create timer.\n"); return false; } asteroids.event_queue = al_create_event_queue(); if(!asteroids.event_queue) { fprintf(stderr, "failed to create event queue.\n"); return false; } if(FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); asteroids.display = al_create_display(SCREEN_W, SCREEN_H); if(!asteroids.display) { fprintf(stderr, "failed to create display.\n"); return false; } /* TODO: show on mouse movement */ al_hide_mouse_cursor(asteroids.display); al_register_event_source(asteroids.event_queue, al_get_display_event_source(asteroids.display)); al_register_event_source(asteroids.event_queue, al_get_timer_event_source(asteroids.timer)); al_register_event_source(asteroids.event_queue, al_get_keyboard_event_source()); return true; }