Guest User

Untitled

a guest
Jan 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. void RenderMap(GameState *state, SDL_Renderer *renderer)
  2. {
  3. if (state && renderer && state->map && state->tile)
  4. {
  5. /*
  6. Get map dim
  7. 80x60px x 11x11 map - 880x660
  8. Bottom of the last tile extends 36 pixels
  9. 880x696 fit to 1280x720
  10. */
  11. state->map_rect = { 0, 0, state->tile_width*state->map->w, (state->tile_height*state->map->h) + 36 };
  12. state->map_rect.x = (state->render_buffer->w - state->map_rect.w) / 2;
  13. state->map_rect.y = (state->render_buffer->h - state->map_rect.h) / 2;
  14.  
  15. // Debug: Draw map dim
  16. SDL_Rect dest = { state->map_rect.x, state->map_rect.y, state->map_rect.w, state->map_rect.h };
  17. SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
  18. SDL_RenderDrawRect(renderer, &dest);
  19.  
  20. // Draw map
  21. u32 tile_id = 0;
  22.  
  23. // Start at the top/center of the map rect
  24. s32 start_x = state->map_rect.x + (state->map_rect.w / 2) - (state->full_tile_width / 2);
  25. s32 start_y = state->map_rect.y;
  26. dest = { start_x, start_y, state->full_tile_width, state->full_tile_height };
  27.  
  28. for (s32 y = 0; y < state->map->h; y++)
  29. {
  30. for (s32 x = 0; x < state->map->w; x++)
  31. {
  32. tile_id = state->map->data[(state->map->w * y) + x];
  33. SDL_RenderCopy(renderer, state->tile[tile_id]->texture, 0, &dest);
  34.  
  35. // Debug - Draw selected tile by mouse hover
  36. // Get relative mouse coord
  37. s32 rel_mouse_x = state->input.mouse_uv.x * state->render_buffer->w;
  38. s32 rel_mouse_y = state->input.mouse_uv.y * state->render_buffer->h;
  39. // Bounds check current rendering tile...
  40. 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)
  41. {
  42. // Do pixel perfect check with alpha channel
  43. if (CheckBitmask({ rel_mouse_x - dest.x, rel_mouse_y - dest.y }, state->tile_selection))
  44. {
  45. SDL_Rect sel_dest = { dest.x, dest.y, state->tile_selection->w, state->tile_selection->h };
  46. SDL_RenderCopy(renderer, state->tile_selection->texture, 0, &sel_dest);
  47. }
  48. }
  49. // < />
  50.  
  51. dest.x += state->full_tile_width / 2;
  52. dest.y += state->tile_height/2;
  53. }
  54.  
  55. dest.x = start_x - ((state->full_tile_width / 2) * (y+1));
  56. dest.y = start_y + ((y+1) * (state->tile_height/2));
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment