summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoresquizo <esquizo+noreply@esquizo.net>2026-04-21 15:40:29 -0300
committeresquizo <esquizo+noreply@esquizo.net>2026-04-21 15:40:29 -0300
commit83d040a27fee8b0cd9933c08e9a6bcaecb17a2db (patch)
tree72092741e1bf769a61c2ca93ca5f3d5aba253adf
parent7c67cdaf11938f1fa735858da531bcd7e3cc2c5f (diff)
adicionado camera
-rw-r--r--sdl3_main.c44
1 files changed, 34 insertions, 10 deletions
diff --git a/sdl3_main.c b/sdl3_main.c
index 1a20d15..83be4ab 100644
--- a/sdl3_main.c
+++ b/sdl3_main.c
@@ -1,4 +1,3 @@
-#include <SDL3/SDL_rect.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
@@ -36,6 +35,8 @@ static Player player;
static Ghost ghosts[1024];
static Bullet bullet[1024];
+static float camera_x, camera_y;
+
static bool shot;
static int shotx, shoty;
@@ -81,13 +82,37 @@ main()
void
render(void)
{
+ {
+ float plx, ply, minx, miny, maxx, maxy;
+
+ plx = player.x - camera_x;
+ ply = player.y - camera_y;
+
+ minx = plx - 80;
+ miny = ply - 80;
+ maxx = plx + 80;
+ maxy = ply + 80;
+
+ if(minx < 0)
+ camera_x += minx;
+
+ if(miny < 0)
+ camera_y += miny;
+
+ if(maxx > 800)
+ camera_x += (maxx - 800);
+
+ if(maxy > 600)
+ camera_y += (maxy - 600);
+ }
+
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderRect(renderer, &(SDL_FRect){
- .x = player.x - 15,
- .y = player.y - 15,
+ .x = player.x - 15 - camera_x,
+ .y = player.y - 15 - camera_y,
.w = 30,
.h = 30
});
@@ -98,8 +123,8 @@ render(void)
SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
SDL_RenderRect(renderer, &(SDL_FRect){
- .x = ghosts[i].x - 15,
- .y = ghosts[i].y - 15,
+ .x = ghosts[i].x - 15 - camera_x,
+ .y = ghosts[i].y - 15 - camera_y,
.w = 30,
.h = 30
});
@@ -111,8 +136,8 @@ render(void)
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
SDL_RenderRect(renderer, &(SDL_FRect){
- .x = bullet[i].x - 2,
- .y = bullet[i].y - 2,
+ .x = bullet[i].x - 2 - camera_x,
+ .y = bullet[i].y - 2 - camera_y,
.w = 4,
.h = 4
});
@@ -255,11 +280,10 @@ process_events()
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
shot = true;
- shotx = event.button.x;
- shoty = event.button.y;
+ shotx = event.button.x + camera_x;
+ shoty = event.button.y + camera_y;
break;
}
}
}
-