int main() { //struct timeval lastTime; WINDOW *win = initscr(); keypad(win, true); curs_set(0); raw(); noecho(); timeout(gSpeed); start_color(); // gGame.width = getmaxx(win); gGame.height = getmaxy(win); level_load(0); //gettimeofday(&gGame.lastTime, NULL); while ((gGame.input = getch()) != 'q') { level_draw(); /*gettimeofday(&lastTime, NULL); gGame.elapsedTime = 1000 * (gGame.lastTime.tv_sec - lastTime.tv_sec) + (gGame.lastTime.tv_usec - lastTime.tv_usec); gGame.lastTime = lastTime;*/ } level_unload(true); keypad(win, false); curs_set(1); echo(); noraw(); delwin(win); endwin(); return 0; }
void level_load(const char *path) { struct level *level; char *bg_path,*mask_path,*settings_path; FILE *settings; if (current_level) level_unload(); obj_type_clear_counts(); level = xcalloc(1, sizeof(*level)); level->path = path; bg_path = strjoin3("levels/",level->path,"/bg.jpg"); mask_path = strjoin3("levels/",level->path,"/mask.png"); settings_path = strjoin3("levels/",level->path,"/settings"); level->name = "Untitled Level"; level->gravity = 0.05; level->bounce = 0; level->ground_friction = 0.03; current_level = level; settings = fopen(settings_path,"r"); if (!settings) { fprintf(stderr,"Error: level (file '%s') not found!\n", settings_path); exit(EXIT_FAILURE); } shell_eval_file(settings); fclose(settings); fprintf(stderr,"Loading level %s\n",level->name); level->bg_image = image_load(bg_path,mask_path); level->bg_sprite.image = level->bg_image; level->bg_sprite.x = 0; level->bg_sprite.y = 0; level->width = level->bg_image->img->w; level->height = level->bg_image->img->h; free(bg_path); free(mask_path); free(settings_path); level->state = LEVEL_STATE_INPROGRESS; if (level->init) level->init(level); display_set_background(level->bg_image->img); }
void level_load(unsigned int level) { level_unload(false); gLevel.width = LEVELS[level].width; gLevel.height = LEVELS[level].height; gLevel.tiles = realloc(gLevel.tiles, sizeof(gLevel.tiles[0]) * gLevel.width * gLevel.height); gLevel.specialCount = 0; gLevel.portalsCount = 0; for (portal = 0; portal < PORTAL_MAX; ++portal) { if (PORTALS[portal].from == level) { gLevel.portals = realloc(gLevel.portals, sizeof(gLevel.portals[0]) * (gLevel.portalsCount+1)); gLevel.portals[gLevel.portalsCount].level = PORTALS[portal].level; gLevel.portals[gLevel.portalsCount].destination = PORTALS[portal].destination; ++gLevel.portalsCount; } } portal = 0; gLevel.enemyCount = 0; enemy = 0; unsigned int tile = 0; for (unsigned int tile_y = 0; tile_y < gLevel.height; ++tile_y) { for (unsigned int tile_x = 0; tile_x < gLevel.width; ++tile_x) { gLevel.tiles[tile].frame = 0; gLevel.tiles[tile].frames = NULL; gLevel.tiles[tile].collision = NULL; char ch = LEVELS[level].tiles[tile]; for (unsigned int i = 0; i < TILES_SPECIAL_MAX; ++i) { if (ch == TILES_SPECIAL[i].tile) { if (TILES_SPECIAL[i].init != NULL) { char init = TILES_SPECIAL[i].init(tile_x, tile_y); if (init != 0) ch = init; } if (TILES_SPECIAL[i].tick != NULL) { ++gLevel.specialCount; gLevel.specials = realloc(gLevel.specials, sizeof(gLevel.specials[0]) * gLevel.specialCount); gLevel.specials[gLevel.specialCount-1].tick = TILES_SPECIAL[i].tick; } if (TILES_SPECIAL[i].collision != NULL) { gLevel.tiles[tile].collision = TILES_SPECIAL[i].collision; } break; } } for (unsigned int i = 0; i < TILES_ANIM_MAX; ++i) { if (ch == TILES_ANIM[i].frames[0]) { gLevel.tiles[tile].frames = malloc(sizeof(char) * (strlen(TILES_ANIM[i].frames) + 1)); strcpy(gLevel.tiles[tile].frames, TILES_ANIM[i].frames); break; } } if (gLevel.tiles[tile].frames == NULL) { gLevel.tiles[tile].frames = malloc(sizeof(char) * 2); gLevel.tiles[tile].frames[0] = ch; gLevel.tiles[tile].frames[1] = 0; } ++tile; } } }