void test_update() { game_set_scale(2.0f); renderer_start(); render_inventory(&inventory, v2(16, 16)); renderer_draw(); }
static INLINE void renderer_draw_conditional(struct xa_context *r, int next_batch) { if (r->buffer_size + next_batch >= XA_VB_SIZE || (next_batch == 0 && r->buffer_size)) { renderer_draw(r); } }
static INLINE void renderer_draw_conditional(struct xorg_renderer *r, int next_batch) { if (r->buffer_size + next_batch >= BUF_SIZE || (next_batch == 0 && r->buffer_size)) { renderer_draw(r); } }
void main_menu_update() { game_set_scale(2.0f); renderer_start(); Sprite s; init_sprite(&s); s.size = v2(15 * 16, 4 * 16); s.position = v2(16 + s.size.x / 2, game->size.y / 2 - s.size.y / 2); s.texture = Get_Texture_Coordinates( renderer->texture_width - s.size.x, 2 * 16, s.size.x, s.size.y); renderer_push_sprite(&s); if(gui_add_button(v2(40 + 16, s.position.y + s.size.y / 2 + 16), "Start")) { game->state = Game_State_Play; } renderer_draw(); }
void update_world_area(World_Area* area) { game_set_scale(2.0); real movespeed = 800; Vec2 move_impulse = v2(0, 0); if(_check(LEFT, A, State_Pressed)) { move_impulse.x -= movespeed; } if(_check(RIGHT, D, State_Pressed)) { move_impulse.x += movespeed; } if(_check(UP, W, State_Pressed)) { move_impulse.y -= movespeed; } if(_check(DOWN, S, State_Pressed)) { move_impulse.y += movespeed; } if(fabsf(move_impulse.x * move_impulse.y) > 0.01f) { move_impulse *= Math_InvSqrt2; } play_state->current_time = SDL_GetTicks(); real dt = (play_state->current_time - play_state->prev_time) / 1000.0; dt = clamp(dt, 0, 1.2f); play_state->accumulator += dt; play_state->prev_time = play_state->current_time; sim_sort_bodies_on_id(&area->sim); Entity* player_entity = world_area_find_entity(area, 0); Sim_Body* player = player_entity->body; Tile_Info* player_tile = area->map.info + tilemap_get_at(&area->map, player->shape.center); move_impulse *= player_tile->movement_modifier; while(play_state->accumulator >= Time_Step) { play_state->accumulator -= Time_Step; player->velocity += move_impulse; sim_update(&area->sim, &area->map, Time_Step); } Direction old_direction = player_entity->direction; if(move_impulse.y < 0) { player_entity->direction = Direction_North; } else if(move_impulse.y > 0) { player_entity->direction = Direction_South; } if(move_impulse.x < 0) { player_entity->facing = -1; player_entity->direction = Direction_West; } else if(move_impulse.x > 0) { player_entity->facing = 1; player_entity->direction = Direction_East; } if(input->scancodes[SDL_SCANCODE_SPACE] == State_Pressed) { player_entity->direction = old_direction; } Sprite* plr_spr = &player_entity->sprite; int32 player_frame = 0; if(v2_dot(move_impulse, move_impulse) > 0){ player_entity->counter++; player_frame = 1; if(player_entity->counter > 15) { player_frame = 0; if(player_entity->counter > 30) { player_entity->counter = 0; } } } else { player_entity->counter = 0; player_frame = 0; } if(player_entity->facing == -1) { plr_spr->texture = Get_Texture_Coordinates(32 + player_frame * 32, 0, -32, 32); } else if(player_entity->facing == 1) { plr_spr->texture = Get_Texture_Coordinates(0 + player_frame * 32, 0, 32, 32); } Vec2 target = player->shape.center; if(target.x < 0) { world_switch_current_area(play_state->world, area->west); play_state->world_xy.x--; } else if(target.x > area->map.w * Tile_Size) { world_switch_current_area(play_state->world, area->east); play_state->world_xy.x++; } else if(target.y < 0) { world_switch_current_area(play_state->world, area->north); play_state->world_xy.y--; } else if(target.y > area->map.h * Tile_Size) { world_switch_current_area(play_state->world, area->south); play_state->world_xy.y++; } area->offset += (target - area->offset) * 0.1f; area->offset -= game->size * 0.5f; if(area->offset.x < 0) area->offset.x = 0; else if((area->offset.x + game->size.x) > area->map.w * Tile_Size) area->offset.x = area->map.w * Tile_Size - game->size.x; if(area->offset.y < 0) area->offset.y = 0; else if((area->offset.y + game->size.y) > area->map.h * Tile_Size) area->offset.y = area->map.h * Tile_Size - game->size.y; //TODO(will) refactor into own function? /* * Player input code * */ if(input->mouse[SDL_BUTTON_LEFT] == State_Just_Pressed) { Entity* ball_entity = world_area_get_next_entity(area); Sim_Body* ball = ball_entity->body; Vec2 dmouse = v2(input->mouse_x / game->scale, input->mouse_y / game->scale) + area->offset; dmouse -= player->shape.center; real angle = atan2f(dmouse.y, dmouse.x); Vec2 normal = v2(cosf(angle), sinf(angle)); entity_add_event_on_activate(ball_entity, delete_on_activate); ball->damping = 0.9999f; ball->shape.hext = v2(8, 16); ball->shape.center = normal * ball->shape.hw * 4 + player->shape.center; ball->velocity += normal * 2000; ball->shape.hext = v2(8, 6); //ball->flags = Body_Flag_No_Friction; ball_entity->sprite.size = v2(16, 32); ball_entity->sprite.center = v2(0, 10); ball_entity->sprite.texture = Get_Texture_Coordinates(0, 96, 32, 64); } if(input->mouse[SDL_BUTTON_RIGHT] == State_Just_Pressed) { Vec2 dmouse = v2( input->mouse_x / game->scale, input->mouse_y / game->scale) + area->offset; AABB mbb = aabb(dmouse, 0, 0); world_area_synchronize_entities_and_bodies(area); for(isize i = 0; i < area->entities_count; ++i) { Entity* e = area->entities + i; if(aabb_intersect(e->body->shape, mbb)) { if(e->id != 0) { for(isize j = 0; j < e->event_on_activate_count; ++j) { e->event_on_activate[j](e, area); } break; } } } } if(input->scancodes[SDL_SCANCODE_F] == State_Just_Pressed) { //tilemap_set_at(&area->map, player->shape.center, Tile_Dug_Earth); Tile_State* state = tilemap_get_state_at(&area->map, player->shape.center); if(state != NULL) { state->damage++; update_tile_state_at(&area->map, player->shape.center); } } Sprite s; if(input->scancodes[SDL_SCANCODE_SPACE] >= State_Pressed) { init_sprite(&s); s.position = player->shape.center; s.size = v2(16, 16); s.texture = Get_Texture_Coordinates(0, renderer->texture_height - 16, 16, 16); s.color = v4(1, 1, 1, 1); switch(player_entity->direction) { case Direction_North: s.position.y -= s.size.y + player->shape.hh; break; case Direction_South: s.position.y += s.size.y + player->shape.hh; break; case Direction_East: s.position.x += s.size.x + player->shape.hw; break; case Direction_West: s.position.x -= s.size.x + player->shape.hh; break; } if(input->scancodes[SDL_SCANCODE_SPACE] == State_Just_Pressed) { //TODO(will) implement good space queries Sim_Body* touching = sim_query_aabb(&area->sim, aabb(s.position, s.size.x / 2, s.size.y / 2)); if(touching != NULL) { if(!Has_Flag(touching->flags, Body_Flag_Static)) player_entity->held_entity_id = touching->entity_id; } } } else { player_entity->held_entity_id = -1; } char debug_str[256]; if(player_entity->held_entity_id > 0) { Entity* e = world_area_find_entity(area, player_entity->held_entity_id); if(e != NULL) { Sim_Body* b = e->body; Vec2 target = player->shape.center; Vec2 diff = b->shape.hext + player->shape.hext + v2(8, 8); switch(player_entity->direction) { case Direction_North: target.y -= diff.y; break; case Direction_South: target.y += diff.y; break; case Direction_East: target.x += diff.x; break; case Direction_West: target.x -= diff.x; break; } Vec2 impulse = (target - b->shape.center); if(v2_dot(impulse, impulse) > (4 * Tile_Size * Tile_Size)) { player_entity->held_entity_id = -1; } impulse *= 60; { snprintf(debug_str, 256, "T(%.2f %.2f) j(%.2f %.2f)", target.x, target.y, impulse.x, impulse.y); } if(v2_dot(impulse, impulse) < (1000 * 1000)) b->velocity += impulse;// * b->inv_mass; } } renderer->offset = area->offset; area->offset += game->size * 0.5f; // throw a ball renderer_start(); Rect2 screen = rect2( area->offset.x - game->size.x / 2, area->offset.y - game->size.y / 2, game->size.x, game->size.y); isize sprite_count_offset = render_tilemap(&area->map, v2(0,0), screen); for(isize i = 0; i < area->entities_count; ++i) { Entity* e = area->entities + i; Sim_Body* b = sim_find_body(&area->sim, e->body_id); if (b == NULL) continue; e->sprite.position = b->shape.center; //e->sprite.size = v2(b->shape.hw * 2, b->shape.hh * 2); //TODO(will) align entity sprites by their bottom center renderer_push_sprite(&e->sprite); } renderer_push_sprite(&s); renderer_sort(sprite_count_offset); renderer_draw(); renderer->offset = v2(0, 0); game_set_scale(1.0); renderer_start(); char str[256]; isize len = snprintf(str, 256, "X:%d Y:%d Tile: %s", play_state->world_xy.x, play_state->world_xy.y, player_tile->name); render_body_text(str, v2(16, game->size.y - (body_font->glyph_height) - 8), true); render_body_text(debug_str, v2(16, 16), true); renderer_draw(); }
int main( void ) { sgui_widget *butt, *check, *check2, *label; sgui_window_description desc; sgui_window* subwnd; sgui_window* subwnd2; sgui_context* ctx; sgui_window* wnd; int selection; puts( "Select rendering backend: " ); puts( " 1) OpenGL(R) old" ); puts( " 2) OpenGL(R) 3.0+ core" ); puts( " 3) Direct3D(R) 9" ); puts( " 4) Direct3D(R) 11" ); puts( "\n 0) quit\n" ); selection = 0; scanf( "%d", &selection ); switch( selection ) { case 1: selection = SGUI_OPENGL_COMPAT; break; case 2: selection = SGUI_OPENGL_CORE; break; case 3: selection = SGUI_DIRECT3D_9; break; case 4: selection = SGUI_DIRECT3D_11; break; default: return 0; } sgui_init( ); /* create a window */ memset( &desc, 0, sizeof(desc) ); desc.width = WIDTH; desc.height = HEIGHT; desc.flags = SGUI_FIXED_SIZE|SGUI_DOUBLEBUFFERED; desc.backend = selection; desc.bits_per_pixel = 32; desc.depth_bits = 16; wnd = sgui_window_create_desc( &desc ); sgui_window_set_title( wnd, "subwm" ); sgui_window_move_center( wnd ); sgui_window_set_visible( wnd, SGUI_VISIBLE ); sgui_window_make_current( wnd ); sgui_window_set_vsync( wnd, 1 ); ctx = sgui_window_get_context( wnd ); renderer_init( selection, ctx ); /* */ wm = sgui_ctx_wm_create( wnd ); sgui_window_set_userptr( wnd, wm ); sgui_window_on_event( wnd, (sgui_window_callback)sgui_ctx_wm_inject_event ); /* create some sub windows */ subwnd = sgui_ctx_wm_create_window( wm, 256, 128, 0 ); subwnd2 = sgui_ctx_wm_create_window( wm, 256, 128, 0 ); sgui_window_set_visible( subwnd, 1 ); sgui_window_set_visible( subwnd2, 1 ); sgui_window_move( subwnd2, 10, 10 ); sgui_window_set_title( subwnd, "Sub Window" ); sgui_window_set_title( subwnd2, "Another Window" ); /* create a few widgets */ butt = sgui_button_create( 10, 35, 60, 25, "Button", 0 ); check = sgui_checkbox_create( 10, 65, "OpenGL" ); check2 = sgui_checkbox_create( 10, 90, "Texture" ); label = sgui_label_create( 10, 35, "Hello, world!\n\n" "From a <b><i>sub</i></b> window." ); sgui_button_set_state( check, 1 ); sgui_button_set_state( check2, 1 ); /* add the widgets to the sub windows */ sgui_window_add_widget( subwnd, butt ); sgui_window_add_widget( subwnd, check ); sgui_window_add_widget( subwnd, check2 ); sgui_window_add_widget( subwnd2, label ); /* main loop */ while( sgui_main_loop_step( ) ) { renderer_draw( selection, ctx ); if( drawgui ) sgui_ctx_wm_draw_gui( wm ); sgui_window_swap_buffers( wnd ); } /* clean up */ sgui_window_destroy( subwnd ); sgui_window_destroy( subwnd2 ); sgui_widget_destroy( butt ); sgui_widget_destroy( check ); sgui_widget_destroy( check2 ); sgui_widget_destroy( label ); sgui_ctx_wm_destroy( wm ); sgui_window_release_current( wnd ); sgui_window_destroy( wnd ); sgui_deinit( ); return 0; }