quick'n dirty offset system

This commit is contained in:
Shadow15510 2021-08-17 23:08:50 +02:00
parent a5c35c6f2b
commit da17beef2a
8 changed files with 82 additions and 57 deletions

View File

@ -12,18 +12,18 @@ find_package(Gint 2.1 REQUIRED)
set(SOURCES
src/main.c
src/game_engine.c
# ...
src/display_engine.c
)
# Shared assets, fx-9860G-only assets and fx-CG-50-only assets
set(ASSETS
# ...
)
set(ASSETS_fx
)
set(ASSETS_cg
assets-cg/tileset.png
assets-cg/maps/world.json
)
fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA)

View File

@ -1,14 +0,0 @@
{ "columns":49,
"image":"..\/tileset.png",
"imageheight":373,
"imagewidth":832,
"margin":0,
"name":"tileset",
"spacing":1,
"tilecount":1078,
"tiledversion":"1.7.1",
"tileheight":16,
"tilewidth":16,
"type":"tileset",
"version":"1.6"
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.2" name="tileset" tilewidth="16" tileheight="16" spacing="1" tilecount="1078" columns="49">
<image source="../tileset.png" width="832" height="373"/>
</tileset>

View File

@ -1 +1,26 @@
#include "display_engine.h"
#include "display_engine.h"
void display_map(const int x_offset, const int y_offset)
{
extern const struct map map_world;
extern const bopti_image_t img_tileset;
for (int layer = 0 ; layer < map_world.nb_layers; layer++)
{
for (int y = 0 ; y <= map_world.height; y ++)
{
for (int x = 0 ; x <= map_world.width; x ++)
{
unsigned int tile_id = map_world.layers[layer][y * map_world.width + x];
if (tile_id != 0)
{
tile_id --;
unsigned int tile_x = (TILE_SIZE + 1) * (tile_id % TILESET_WIDTH);
unsigned int tile_y = (TILE_SIZE + 1) * (tile_id / TILESET_WIDTH);
dsubimage(x_offset + x * TILE_SIZE, y_offset + y * TILE_SIZE, &img_tileset, tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE);
}
}
}
}
}

View File

@ -1,5 +1,23 @@
#ifndef _DISPLAY_ENGINE_H
#define _DISPLAY_ENGINE_H
#include <gint/display.h>
#define TILE_SIZE 16
#define TILESET_WIDTH 49
#define SCREEN_WIDTH 24
#define SCREEN_HEIGHT 14
struct map
{
int width, height, nb_layers;
// to get the (x; y) cell : layers[layer_number][y * map_width + x]
uint16_t *layers[];
};
// display_map : show the current map with x, y offsets
void display_map(const int x_offset, const int y_offset);
#endif /* _DISPLAY_ENGINE_H */

View File

@ -1,3 +1,23 @@
#include "game_engine.h"
void keyboard_manager(const int key, int *x_offset, int *y_offset)
{
switch(key)
{
case KEY_UP:
*y_offset -= 4;
break;
case KEY_RIGHT:
*x_offset += 4;
break;
case KEY_DOWN:
*y_offset += 4;
break;
case KEY_LEFT:
*x_offset -= 4;
break;
}
}

View File

@ -3,12 +3,6 @@
#include <gint/keyboard.h>
struct map
{
int width, height, nb_layers;
// to get a cell : layers[layer_number][y*width+x]
uint16_t *layers[];
};
void keyboard_manager(const int key, int *x_offset, int *y_offset);
#endif /* _GAME_ENGINE_H */

View File

@ -6,45 +6,23 @@
GNU General Public Licence v3.0
*/
#include <gint/keyboard.h>
#include <gint/display.h>
#include "game_engine.h"
#define TILE_SIZE 16
#define TILESET_WIDTH 49
#define SCREEN_WIDTH 24
#define SCREEN_HEIGHT 14
#include "display_engine.h"
int main(void)
{
extern const struct map map_world;
extern const bopti_image_t img_tileset;
int key = 0, x_offset = 0, y_offset = 0;
dclear(C_WHITE);
for (int layer = 0 ; layer < map_world.nb_layers; layer++)
while (key != KEY_EXIT)
{
for (int y = 0 ; y <= SCREEN_HEIGHT; y ++)
{
for (int x = 0 ; x <= SCREEN_WIDTH; x ++)
{
unsigned int tile_id = map_world.layers[layer][y * map_world.width + x];
if (tile_id != 0)
{
tile_id --;
unsigned int tile_x = (TILE_SIZE + 1) * (tile_id % TILESET_WIDTH);
unsigned int tile_y = (TILE_SIZE + 1) * (tile_id / TILESET_WIDTH);
dsubimage(x * TILE_SIZE, y * TILE_SIZE, &img_tileset, tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE);
}
}
}
dclear(C_WHITE);
display_map(x_offset, y_offset);
dprint(2, 2, C_BLACK, "(%d, %d)", x_offset, y_offset);
dupdate();
key = getkey().key;
keyboard_manager(key, &x_offset, &y_offset);
}
// dprint(2, 2, C_BLACK, "%d", map_world.layers[0][0]);
dupdate();
getkey();
return 0;
}