Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void RenderMap(GameState *state, SDL_Renderer *renderer)
- {
- if (state && renderer && state->map && state->tile)
- {
- /*
- Get map dim
- 80x60px x 11x11 map - 880x660
- Bottom of the last tile extends 36 pixels
- 880x696 fit to 1280x720
- */
- state->map_rect = { 0, 0, state->tile_width*state->map->w, (state->tile_height*state->map->h) + 36 };
- state->map_rect.x = (state->render_buffer->w - state->map_rect.w) / 2;
- state->map_rect.y = (state->render_buffer->h - state->map_rect.h) / 2;
- // Debug: Draw map dim
- SDL_Rect dest = { state->map_rect.x, state->map_rect.y, state->map_rect.w, state->map_rect.h };
- SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
- SDL_RenderDrawRect(renderer, &dest);
- // Draw map
- u32 tile_id = 0;
- // Start at the top/center of the map rect
- s32 start_x = state->map_rect.x + (state->map_rect.w / 2) - (state->full_tile_width / 2);
- s32 start_y = state->map_rect.y;
- dest = { start_x, start_y, state->full_tile_width, state->full_tile_height };
- for (s32 y = 0; y < state->map->h; y++)
- {
- for (s32 x = 0; x < state->map->w; x++)
- {
- tile_id = state->map->data[(state->map->w * y) + x];
- SDL_RenderCopy(renderer, state->tile[tile_id]->texture, 0, &dest);
- // Debug - Draw selected tile by mouse hover
- // Get relative mouse coord
- s32 rel_mouse_x = state->input.mouse_uv.x * state->render_buffer->w;
- s32 rel_mouse_y = state->input.mouse_uv.y * state->render_buffer->h;
- // Bounds check current rendering tile...
- if (rel_mouse_x >= dest.x && rel_mouse_x <= dest.x + dest.w && rel_mouse_y >= dest.y && rel_mouse_y <= dest.y + dest.h)
- {
- // Do pixel perfect check with alpha channel
- if (CheckBitmask({ rel_mouse_x - dest.x, rel_mouse_y - dest.y }, state->tile_selection))
- {
- SDL_Rect sel_dest = { dest.x, dest.y, state->tile_selection->w, state->tile_selection->h };
- SDL_RenderCopy(renderer, state->tile_selection->texture, 0, &sel_dest);
- }
- }
- // < />
- dest.x += state->full_tile_width / 2;
- dest.y += state->tile_height/2;
- }
- dest.x = start_x - ((state->full_tile_width / 2) * (y+1));
- dest.y = start_y + ((y+1) * (state->tile_height/2));
- }
- }
- }
Add Comment
Please, Sign In to add comment