got my shit in order regarding tilemaps
parent
544e604349
commit
e6a011c12b
|
@ -209,9 +209,7 @@ sprite_t make_sprite(const char* file, float x, float y) {
|
||||||
.sx=1.0,.sy=1.0,
|
.sx=1.0,.sy=1.0,
|
||||||
.rot=0,
|
.rot=0,
|
||||||
.depth=RLAYER_SPRITES,
|
.depth=RLAYER_SPRITES,
|
||||||
.uv=(SDL_Rect){
|
.uv=(SDL_Rect){0,0,0,0}
|
||||||
.x=0,.y=0,.w=0,.h=0
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
SDL_QueryTexture(sprite.texture, NULL, NULL, &sprite.uv.w, &sprite.uv.h);
|
SDL_QueryTexture(sprite.texture, NULL, NULL, &sprite.uv.w, &sprite.uv.h);
|
||||||
sprite.origin.x = -(float)sprite.uv.h/2.f; sprite.origin.y = -(float)sprite.uv.h/2.f;
|
sprite.origin.x = -(float)sprite.uv.h/2.f; sprite.origin.y = -(float)sprite.uv.h/2.f;
|
||||||
|
@ -233,8 +231,9 @@ sprite_t sprite_from_spritesheet(spritesheet_t *sheet, int index) {
|
||||||
|
|
||||||
SDL_Rect get_srcrect_from(spritesheet_t *sheet, int index) {
|
SDL_Rect get_srcrect_from(spritesheet_t *sheet, int index) {
|
||||||
int pixels = index * sheet->tile_width;
|
int pixels = index * sheet->tile_width;
|
||||||
|
int w = sheet->w / sheet->tile_width;
|
||||||
return (SDL_Rect) {
|
return (SDL_Rect) {
|
||||||
pixels%sheet->w, pixels/sheet->w*sheet->tile_height,
|
pixels%sheet->w, index/w * sheet->tile_height,
|
||||||
sheet->tile_width, sheet->tile_height,
|
sheet->tile_width, sheet->tile_height,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
43
src/game.cc
43
src/game.cc
|
@ -1,18 +1,16 @@
|
||||||
#include "engine.hpp"
|
#include "engine.hpp"
|
||||||
|
#include "tilemap.hpp"
|
||||||
#include "corelib/input.h"
|
#include "corelib/input.h"
|
||||||
#include "corelib/render.h"
|
#include "corelib/render.h"
|
||||||
#include "corelib/assets.h"
|
#include "corelib/assets.h"
|
||||||
#include "corelib/layers.h"
|
#include "corelib/layers.h"
|
||||||
#include "tilemap.hpp"
|
|
||||||
#include "SDL2/SDL_mouse.h"
|
#include "SDL2/SDL_mouse.h"
|
||||||
#include <SDL2/SDL_scancode.h>
|
#include <SDL2/SDL_scancode.h>
|
||||||
|
|
||||||
void on_cycle_tile(int);
|
|
||||||
|
|
||||||
int cursor_tile = 1;
|
int cursor_tile = 1;
|
||||||
int dragging = 0;
|
int dragging = 0;
|
||||||
int drawing = 0;
|
int drawing = 0;
|
||||||
sprite_t player;
|
sprite_t cursor;
|
||||||
sprite_t current_tile_display;
|
sprite_t current_tile_display;
|
||||||
spritesheet_t world_sheet;
|
spritesheet_t world_sheet;
|
||||||
|
|
||||||
|
@ -36,7 +34,9 @@ void on_clear_key(int down) {
|
||||||
|
|
||||||
void on_cycle_tile(int axis) {
|
void on_cycle_tile(int axis) {
|
||||||
cursor_tile += axis;
|
cursor_tile += axis;
|
||||||
player.uv = get_srcrect_from(&world_sheet, cursor_tile);
|
if(g_tilemap.tileset.set.size() >= cursor_tile && 0 < cursor_tile) {
|
||||||
|
cursor = g_tilemap.tileset.set.at(cursor_tile-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_quit_key(int down) {
|
void on_quit_key(int down) {
|
||||||
|
@ -63,20 +63,28 @@ void on_middle_mouse(int down) {
|
||||||
void load_game() {
|
void load_game() {
|
||||||
g_tilemap.width = g_tilemap.height = 10;
|
g_tilemap.width = g_tilemap.height = 10;
|
||||||
|
|
||||||
player = make_sprite("tilemap.png", 0, 0);
|
cursor = make_sprite("tilemap.png", 0, 0);
|
||||||
|
|
||||||
current_tile_display = make_sprite("tilemap.png", 0, 0);
|
current_tile_display = make_sprite("tilemap.png", 0, 0);
|
||||||
current_tile_display.depth = 0;
|
current_tile_display.depth = 0;
|
||||||
current_tile_display.sx = 0.15f;
|
current_tile_display.sx = 0.15f;
|
||||||
current_tile_display.sy = 0.15f;
|
current_tile_display.sy = 0.15f;
|
||||||
|
|
||||||
world_sheet = make_spritesheet("tilemap.png", 16, 16);
|
world_sheet = make_spritesheet("tilemap.png", 189, 189);
|
||||||
|
|
||||||
g_tilemap.tileset.set = {
|
g_tilemap.tileset.set = {
|
||||||
|
sprite_from_spritesheet(&world_sheet, 0),
|
||||||
sprite_from_spritesheet(&world_sheet, 1),
|
sprite_from_spritesheet(&world_sheet, 1),
|
||||||
sprite_from_spritesheet(&world_sheet, 2),
|
sprite_from_spritesheet(&world_sheet, 2),
|
||||||
sprite_from_spritesheet(&world_sheet, 3),
|
sprite_from_spritesheet(&world_sheet, 10),
|
||||||
sprite_from_spritesheet(&world_sheet, 4),
|
sprite_from_spritesheet(&world_sheet, 11),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 12),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 20),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 21),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 22),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 30),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 31),
|
||||||
|
sprite_from_spritesheet(&world_sheet, 32),
|
||||||
};
|
};
|
||||||
|
|
||||||
for(int i=0;i<g_tilemap.width*g_tilemap.height;++i) {
|
for(int i=0;i<g_tilemap.width*g_tilemap.height;++i) {
|
||||||
|
@ -103,7 +111,8 @@ void start_game() {
|
||||||
|
|
||||||
void update_game() {
|
void update_game() {
|
||||||
update_input();
|
update_input();
|
||||||
mouse_world_position(&player.x, &player.y);
|
mouse_world_position(&cursor.x, &cursor.y);
|
||||||
|
cursor.x += 0.1f; cursor.y += 0.1f;
|
||||||
|
|
||||||
if(drawing) {
|
if(drawing) {
|
||||||
int x, y;
|
int x, y;
|
||||||
|
@ -118,16 +127,16 @@ void update_game() {
|
||||||
void draw_game() {
|
void draw_game() {
|
||||||
rectshape_t world_outline = {
|
rectshape_t world_outline = {
|
||||||
g_tilemap.x, g_tilemap.y, float(g_tilemap.width), float(g_tilemap.height),
|
g_tilemap.x, g_tilemap.y, float(g_tilemap.width), float(g_tilemap.height),
|
||||||
0.1f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255}
|
0.01f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255}
|
||||||
};
|
};
|
||||||
rectshape_t cursor_outline = {
|
rectshape_t cursor_outline = {
|
||||||
player.x, player.y, 1.0f, 1.0f, 0.05f, RLAYER_SPRITES-1,
|
cursor.x, cursor.y, 1.0f, 1.0f, 0.01f, RLAYER_SPRITES-1,
|
||||||
{0, 0, 0, 0}, {255, 255, 255, 255}
|
{0, 0, 0, 0}, {255, 255, 255, 255}
|
||||||
};
|
};
|
||||||
|
|
||||||
draw_rect(&world_outline);
|
draw_rect(&world_outline);
|
||||||
draw_rect(&cursor_outline);
|
draw_rect(&cursor_outline);
|
||||||
draw_sprite(&player);
|
draw_sprite(&cursor);
|
||||||
draw_tilemap();
|
draw_tilemap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +145,14 @@ void draw_game_ui() {
|
||||||
0.0f, 0.0f, 0.15f, 0.15f, 0, 1,
|
0.0f, 0.0f, 0.15f, 0.15f, 0, 1,
|
||||||
{100,100,100,255}, {0,0,0,0}
|
{100,100,100,255}, {0,0,0,0}
|
||||||
};
|
};
|
||||||
|
int w = world_sheet.w / world_sheet.tile_width,
|
||||||
|
h = world_sheet.h / world_sheet.tile_height,
|
||||||
|
offset_selected = cursor_tile - 1;
|
||||||
|
rectshape_t selected_tile_rect = (rectshape_t) {
|
||||||
|
float(offset_selected % w) * 0.015f, float(offset_selected / h) * 0.015f, 0.015f, 0.015f, 0.001f, -1,
|
||||||
|
{0,0,0,0}, {255, 255, 255, 255}
|
||||||
|
};
|
||||||
|
draw_rect(&selected_tile_rect);
|
||||||
draw_rect(&shape);
|
draw_rect(&shape);
|
||||||
draw_sprite(¤t_tile_display);
|
draw_sprite(¤t_tile_display);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "signal.h"
|
#include "signal.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
tilemap_t g_tilemap = {
|
tilemap_t g_tilemap = {
|
||||||
.width = 0,.height = 0,
|
.width = 0,.height = 0,
|
||||||
|
@ -13,8 +14,8 @@ void draw_tilemap() {
|
||||||
sprite_t sprite;
|
sprite_t sprite;
|
||||||
for(int y=0;y<g_tilemap.height;++y) {
|
for(int y=0;y<g_tilemap.height;++y) {
|
||||||
for(int x=0;x<g_tilemap.width;++x) {
|
for(int x=0;x<g_tilemap.width;++x) {
|
||||||
auto tile = get_tilemap_tile(x, y);
|
tile_t* tile = get_tilemap_tile(x, y);
|
||||||
if(tile->value == 0) continue;
|
if(tile->value <= 0) continue;
|
||||||
sprite = g_tilemap.tileset.set[tile->value-1];
|
sprite = g_tilemap.tileset.set[tile->value-1];
|
||||||
sprite.depth = RLAYER_TILEMAP;
|
sprite.depth = RLAYER_TILEMAP;
|
||||||
sprite.x = x; sprite.y = y;
|
sprite.x = x; sprite.y = y;
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
10,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,1,1,1,1,3,3,3,2,2,1,1,1,1,3,3,2,2,2,2,1,1,3,3,3,2,2,2,2,2,1,3,3,3,2,2,2,2,2,2,3,3,3,2,2,2,2,2,2,2,3,3,2,2,2,2,2,2,2,2,
|
10,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,3,0,0,0,0,0,0,4,5,5,6,0,0,0,0,0,0,7,8,8,9,0,0,0,0,0,0,7,8,8,9,0,0,0,0,0,0,10,11,11,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
Loading…
Reference in New Issue