tilemap rendering

This commit is contained in:
DaniTheSkunk 2023-01-07 13:22:22 +00:00
parent de0e18afa4
commit 5e041ffbf0
6 changed files with 54 additions and 3 deletions

View File

@ -8,9 +8,12 @@ void init() {
sprites[1].tile = 3;
sprites[1].x = 10;
sprites[1].y = 100;
maps[0].tiles[20][1] = 2;
}
void tick() {
sprites[0].x += 1;
sprites[1].x += 2;
maps[0].scrollx += 1;
}

View File

@ -25,15 +25,24 @@
#include "vec2i.h"
#include "window.h"
#define MAX_SPRITES 256
#define TILEMAP_MAX_SIZE 1024
#define MAX_TILEMAPS 4
struct gsa_sprite {
u16 tile;
i32 x, y;
};
#define MAX_SPRITES 256
struct gsa_map {
u16 tiles[TILEMAP_MAX_SIZE][TILEMAP_MAX_SIZE];
i32 w, h;
i32 scrollx, scrolly;
};
extern struct gsa_sprite sprites[MAX_SPRITES];
extern struct sw_image8 *_gfx;
extern struct gsa_map maps[MAX_TILEMAPS];
int gsa_main(int argc, char *argv[]);

View File

@ -16,4 +16,7 @@ typedef i8 bool;
#define true 1
#define false 0
i32 i32_max(i32 v1, i32 v2);
i32 i32_min(i32 v1, i32 v2);
#endif /* GUARD_649E63003BEAE79ECA2B813C4E131C9A */

View File

@ -48,6 +48,7 @@ skunk_sources = [
'src/image8.c',
'src/shader.c',
'src/skunkworks.c',
'src/types.c',
'src/vec2i.c',
'src/window.c'
]

View File

@ -3,12 +3,14 @@
#include "gl.h"
#include "image8.h"
#include "shader.h"
#include "types.h"
#include <gl/gl.h>
#include <string.h>
#define GSA_NOMAIN
#include "gsa.h"
struct gsa_sprite sprites[256];
struct gsa_map maps[MAX_TILEMAPS];
struct sw_image8 *_gfx;
struct sw_window *_win;
@ -159,12 +161,36 @@ static void update_palette_gl() {
}
void _gsa_tick() {
i32 i;
i32 i, x, y, startx, starty, endx, endy;
tick();
next_render_vert = 0;
/* glBegin(GL_TRIANGLES); */
for(i = 0; i < MAX_TILEMAPS; ++i) {
startx = maps[i].scrollx / 16;
starty = maps[i].scrolly / 16;
endx = i32_min(TILEMAP_MAX_SIZE, startx + 20);
endy = i32_min(TILEMAP_MAX_SIZE, starty + 12);
startx = i32_max(0, startx);
starty = i32_max(0, starty);
for(x = startx; x <= endx; ++x) {
for(y = starty; y <= endy; ++y) {
u16 tile = maps[i].tiles[x][y];
if(tile) {
rect(
x * 16 - maps[i].scrollx,
y * 16 - maps[i].scrolly,
16,
16,
tile
);
}
}
}
}
for(i = 0; i < MAX_SPRITES; ++i) {
if(sprites[i].tile > 0) {
rect(sprites[i].x, sprites[i].y, 16, 16, sprites[i].tile);

9
src/types.c Normal file
View File

@ -0,0 +1,9 @@
#include "types.h"
i32 i32_max(i32 v1, i32 v2) {
return v1 > v2 ? v1 : v2;
}
i32 i32_min(i32 v1, i32 v2) {
return v1 < v2 ? v1 : v2;
}