/********************************** Show tiles types **********************************/ static void compose_type(context_t * ctx,int layer_index) { int x = 0; int y = 0; item_t * item; char * type; static TTF_Font * font = NULL; int w; int h; char layer_name[SMALL_BUF]; if( option->show_tile_type == false) { return; } sprintf(layer_name,"%s%d",MAP_KEY_LAYER,layer_index); if( entry_exist(MAP_TABLE, ctx->map, layer_name,MAP_KEY_TYPE,NULL) == false ) { return; } font = font_get(ctx,ITEM_FONT, ITEM_FONT_SIZE); for( x=0; x<default_layer->map_w; x++) { for( y=0; y<default_layer->map_h; y++) { if(entry_read_list_index(MAP_TABLE,ctx->map,&type,x + y * default_layer->map_w,layer_name,MAP_KEY_TYPE,NULL) == RET_NOK ) { continue; } if( type[0] == 0 ) { continue; } item = item_list_add(&item_list); item_set_string(item,type); item_set_font(item,font); sdl_get_string_size(item->font,item->string,&w,&h); item_set_anim_shape(item,map_t2p_x(x,y,default_layer),map_t2p_y(x,y,default_layer),w,h); } } }
/****************************************************** create a list of item for the currently selected screen ******************************************************/ static void compose_scr(context_t * context) { static TTF_Font * font = NULL; option_t * option = option_get(); SDL_SetRenderDrawColor(context->render, 0, 0, 0, 255); switch(current_screen) { case SCREEN_SELECT: item_list = scr_select_compose(context); break; case SCREEN_PLAY: item_list = scr_play_compose(context); break; } if( option->show_fps ) { font = font_get(context,ITEM_FONT, ITEM_FONT_SIZE); frame_rate = item_list_add(&item_list); item_set_font(frame_rate,font); item_set_anim_shape(frame_rate,50,50,20,20); item_set_overlay(frame_rate,1); } }
void screen_play(SDL_Renderer * render,game_t * game) { char gold_buf[128]; char mana_buf[128]; SDL_Event event; int tile_num; int unit_num; int city_num; int encounter_num; int i; int j; int x; LBXAnimation_t * anim_ptr; city_t * city; local_render = render; end_screen = -1; selected_group = NULL; city = (city_t*)game->wiz[0].city->data; cur_tile_x = city->x; cur_tile_y = city->y; load_font(); /* Load resource */ if(anim==NULL) { anim = load_graphics(render,"MAIN.LBX"); if(anim == NULL) { exit(EXIT_FAILURE); } } /* Load tiles */ if(tile==NULL) { tile = load_graphics(render,"TERRAIN.LBX"); if(tile == NULL) { exit(EXIT_FAILURE); } } /* Load unit */ if(unit1==NULL) { unit1 = load_graphics(render,"UNITS1.LBX"); if(unit1 == NULL) { exit(EXIT_FAILURE); } } if(unit2==NULL) { unit2 = load_graphics(render,"UNITS2.LBX"); if(unit2 == NULL) { exit(EXIT_FAILURE); } } /* Concat unit1 and unit2 */ if(unit==NULL) { j=0; anim_ptr = unit1; for(i=0; i<2; i++) { while(anim_ptr->num_frame!=0) { unit = (LBXAnimation_t*)realloc(unit,(j+1)*sizeof(LBXAnimation_t)); memcpy(&unit[j],anim_ptr,sizeof(LBXAnimation_t)); anim_ptr++; j++; } anim_ptr= unit2; } } /* Load back */ if(back==NULL) { back = load_graphics(render,"MAPBACK.LBX"); if(back == NULL) { exit(EXIT_FAILURE); } } j=0; item_init(&item_ui[j]); item_set_frame(&item_ui[j],0,0,&anim[j]); j++; x=7; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); j++; x+=item_ui[j-1].anim->w + 1; item_init(&item_ui[j]); item_set_frame(&item_ui[j],x,4,&anim[j]); item_set_frame_click(&item_ui[j],1); item_set_click_left(&item_ui[j],cb_plane,NULL); j++; /* Main loop */ while( end_screen == -1) { tile_num = draw_tile(game); city_num = draw_city(game); encounter_num = draw_encounter(game); unit_num = draw_unit(game); /* Print gold */ item_init(&item_ui[8]); item_set_font(&item_ui[8],font); sprintf(gold_buf,"%d GP",game->wiz[0].gold); item_set_string(&item_ui[8],gold_buf); item_set_frame(&item_ui[8],264,71,NULL); /* Print mana */ item_init(&item_ui[9]); item_set_font(&item_ui[9],font); sprintf(mana_buf,"%d MP",game->wiz[0].mana); item_set_string(&item_ui[9],mana_buf); item_set_frame(&item_ui[9],300,71,NULL); while (SDL_PollEvent(&event)) { if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE) { return; } /* Move selected_unit */ if(event.type==SDL_KEYDOWN ) { move_unit(game,event.key.keysym.sym); } sdl_screen_manager(&event); sdl_mouse_manager(&event,item_ui,ITM_UI_NUM); sdl_mouse_manager(&event,item_city,city_num); sdl_mouse_manager(&event,item_encounter,encounter_num); sdl_mouse_manager(&event,item_tile,tile_num); sdl_mouse_manager(&event,item_unit,unit_num); sdl_keyboard_manager(&event); } SDL_RenderClear(render); sdl_blit_item_list(item_tile,tile_num); sdl_blit_item_list(item_city,city_num); sdl_blit_item_list(item_encounter,encounter_num); sdl_blit_item_list(item_back,unit_num); sdl_blit_item_list(item_unit,unit_num); sdl_blit_item_list(item_ui,ITM_UI_NUM); sdl_blit_to_screen(); sdl_loop_manager(); } return; }
/********************************** Compose item on map **********************************/ static void compose_item(context_t * ctx,int layer_index) { char * sprite_name = NULL; int sprite_align = ALIGN_CENTER; int sprite_offset_y = 0; anim_t * anim; item_t * item; int x; int y; int temp_x; int temp_y; char ** item_id; int i; static TTF_Font * font = NULL; char * mytemplate; int quantity; char buf[SMALL_BUF]; char layer_name[SMALL_BUF]; sprintf(layer_name,"%s%d",MAP_KEY_LAYER,layer_index); if(entry_get_group_list(MAP_TABLE,ctx->map,&item_id,layer_name,MAP_ENTRY_ITEM_LIST,NULL) == RET_NOK ) { return; } font = font_get(ctx,ITEM_FONT, ITEM_FONT_SIZE); i=0; while( item_id[i] != NULL ) { sprite_align = ALIGN_CENTER; if(entry_read_int(MAP_TABLE,ctx->map,&x,layer_name,MAP_ENTRY_ITEM_LIST,item_id[i],MAP_ITEM_POS_X,NULL) == RET_NOK ) { i++; continue; } if(entry_read_int(MAP_TABLE,ctx->map,&y,layer_name,MAP_ENTRY_ITEM_LIST,item_id[i],MAP_ITEM_POS_Y,NULL) == RET_NOK ) { i++; continue; } mytemplate = item_is_resource(item_id[i]); if ( mytemplate == NULL ) { if(entry_read_string(ITEM_TABLE,item_id[i],&sprite_name,ITEM_SPRITE,NULL) == RET_NOK ) { i++; continue; } entry_read_int(ITEM_TABLE,item_id[i],&sprite_align,ITEM_ALIGN,NULL); entry_read_int(ITEM_TABLE,item_id[i],&sprite_offset_y,ITEM_OFFSET_Y,NULL); } else { if(entry_read_string(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_name,ITEM_SPRITE,NULL) == RET_NOK ) { free(mytemplate); i++; continue; } entry_read_int(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_align,ITEM_ALIGN,NULL); entry_read_int(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_offset_y,ITEM_OFFSET_Y,NULL); free(mytemplate); } item = item_list_add(&item_list); anim = imageDB_get_anim(ctx,sprite_name); free(sprite_name); temp_x = map_t2p_x(x,y,default_layer); temp_y = map_t2p_y(x,y,default_layer); x = temp_x; y = temp_y; /* Align on tile */ if( sprite_align == ALIGN_CENTER ) { x -= ((anim->w*default_layer->map_zoom)-default_layer->tile_width)/2; y -= ((anim->h*default_layer->map_zoom)-default_layer->tile_height)/2; } if( sprite_align == ALIGN_LOWER ) { x -= ((anim->w*default_layer->map_zoom)-default_layer->tile_width)/2; y -= (anim->h*default_layer->map_zoom)-default_layer->tile_height; } y += sprite_offset_y; item_set_pos(item,x,y); item_set_anim(item,anim,0); item_set_zoom_x(item, default_layer->map_zoom ); item_set_zoom_y(item, default_layer->map_zoom ); if(font) { quantity = resource_get_quantity(item_id[i]); sprintf(buf,"%d",quantity); item_set_string(item,buf); item_set_font(item,font); } i++; } deep_free(item_id); }