Avanços
This commit is contained in:
179
main.cpp
179
main.cpp
@@ -6,6 +6,10 @@
|
|||||||
#include "gif.c"
|
#include "gif.c"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define STATE_TILEMAP 0
|
||||||
|
#define STATE_TILEPAL 1
|
||||||
|
#define STATE_MINIMAP 2
|
||||||
|
|
||||||
struct Mouse {
|
struct Mouse {
|
||||||
int x, y;
|
int x, y;
|
||||||
bool buttons[3];
|
bool buttons[3];
|
||||||
@@ -17,11 +21,17 @@ SDL_Window* sdlWindow = NULL;
|
|||||||
SDL_Renderer* sdlRenderer = NULL;
|
SDL_Renderer* sdlRenderer = NULL;
|
||||||
SDL_Texture* sdlFontTexture = NULL;
|
SDL_Texture* sdlFontTexture = NULL;
|
||||||
SDL_Texture* sdlTilesTexture = NULL;
|
SDL_Texture* sdlTilesTexture = NULL;
|
||||||
|
SDL_Texture* sdlMinimapTexture = NULL;
|
||||||
SDL_Event sdlEvent;
|
SDL_Event sdlEvent;
|
||||||
|
SDL_Scancode keyJustPressed = SDL_SCANCODE_UNKNOWN;
|
||||||
|
const Uint8* keyboard = nullptr;
|
||||||
|
Uint32* minitiles = nullptr;
|
||||||
|
bool ignoreMouse = false;
|
||||||
bool anyKey = false;
|
bool anyKey = false;
|
||||||
LoopFun loop;
|
LoopFun loop;
|
||||||
Mouse mouse;
|
Mouse mouse;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
int state = STATE_MINIMAP;
|
||||||
|
|
||||||
int tile_width = 16;
|
int tile_width = 16;
|
||||||
int tile_height = 16;
|
int tile_height = 16;
|
||||||
@@ -35,9 +45,13 @@ char map_filename[100] = { 0 };
|
|||||||
int tilemap_width = 0;
|
int tilemap_width = 0;
|
||||||
int tilemap_height = 0;
|
int tilemap_height = 0;
|
||||||
|
|
||||||
|
unsigned char* map = nullptr;
|
||||||
int map_x = 0;
|
int map_x = 0;
|
||||||
int map_y = 0;
|
int map_y = 0;
|
||||||
|
|
||||||
|
int tile_sel = 0;
|
||||||
|
int tile_back = 0;
|
||||||
|
|
||||||
int StrToInt(const char* str) {
|
int StrToInt(const char* str) {
|
||||||
int val = 0;
|
int val = 0;
|
||||||
int size = strlen(str);
|
int size = strlen(str);
|
||||||
@@ -70,6 +84,7 @@ void LoadConfig() {
|
|||||||
|
|
||||||
void Init() {
|
void Init() {
|
||||||
LoadConfig();
|
LoadConfig();
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_EVERYTHING);
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
sdlWindow = SDL_CreateWindow("Mappy v0.1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width*zoom, screen_height*zoom, SDL_WINDOW_SHOWN);
|
sdlWindow = SDL_CreateWindow("Mappy v0.1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width*zoom, screen_height*zoom, SDL_WINDOW_SHOWN);
|
||||||
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
|
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||||
@@ -89,16 +104,58 @@ void Init() {
|
|||||||
|
|
||||||
int c;
|
int c;
|
||||||
FILE* f = fopen(tiles_filename, "rb");
|
FILE* f = fopen(tiles_filename, "rb");
|
||||||
if (!f) {
|
if (!f) { error = 2; return; }
|
||||||
error = 2;
|
buffer = stbi_load_from_file(f, &tilemap_width, &tilemap_height, &c, 4);
|
||||||
} else {
|
sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
|
||||||
buffer = stbi_load_from_file(f, &tilemap_width, &tilemap_height, &c, 4);
|
SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32));
|
||||||
sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
|
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
|
||||||
SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32));
|
fclose(f);
|
||||||
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
|
|
||||||
stbi_image_free(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const int tmtw = (tilemap_width / tile_width);
|
||||||
|
const int tmth = (tilemap_height / tile_height);
|
||||||
|
minitiles = (Uint32*)malloc(tmtw * tmth*sizeof(Uint32));
|
||||||
|
for (int tmy = 0; tmy < tmth; tmy++) {
|
||||||
|
for (int tmx = 0; tmx < tmth; tmx++) {
|
||||||
|
const int bx = tmx*tile_width;
|
||||||
|
const int by = tmy*tile_height;
|
||||||
|
int r = 0, g = 0, b = 0;
|
||||||
|
for (int ty = 0; ty < tile_width; ty++) {
|
||||||
|
for (int tx = 0; tx < tile_height; tx++) {
|
||||||
|
const Uint32 color = buffer[(bx + tx) + (by + ty)*tilemap_width];
|
||||||
|
r += color & 0xff;
|
||||||
|
g += (color >> 8) & 0xff;
|
||||||
|
b += (color >> 16) & 0xff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r /= tile_width*tile_height;
|
||||||
|
g /= tile_width*tile_height;
|
||||||
|
b /= tile_width*tile_height;
|
||||||
|
minitiles[tmx + tmy*tmtw] = 0xff000000 + r + (b << 8) + (g << 16);
|
||||||
|
//minitiles[tmx + tmy*tmtw] = 0xff + (r << 24) + (b << 8) + (g << 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stbi_image_free(buffer);
|
||||||
|
|
||||||
|
f = fopen(map_filename, "rb");
|
||||||
|
if (!f) { error = 3; return; }
|
||||||
|
unsigned char size[2];
|
||||||
|
fread(&map_width, 1, 1, f);
|
||||||
|
fread(&map_height, 1, 1, f);
|
||||||
|
map = (unsigned char*)malloc(map_width*map_height);
|
||||||
|
fread(map, map_width*map_height, 1, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
sdlMinimapTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, map_width, map_height);
|
||||||
|
pixels = (Uint32*)malloc(map_width * map_height*sizeof(Uint32));
|
||||||
|
for (int y = 0; y < map_height; y++) {
|
||||||
|
for (int x = 0; x < map_width; x++) {
|
||||||
|
const Uint8 tile = map[x + y*map_width];
|
||||||
|
pixels[x + y*map_width] = minitiles[(tile % tmtw) + (tile / tmtw)*tmtw];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SDL_UpdateTexture(sdlMinimapTexture, NULL, pixels, map_width * sizeof(Uint32));
|
||||||
|
|
||||||
|
keyboard = SDL_GetKeyboardState(nullptr);
|
||||||
//SDL_Surface* bmp = SDL_LoadBMP("assets.bmp");
|
//SDL_Surface* bmp = SDL_LoadBMP("assets.bmp");
|
||||||
//texture = SDL_CreateTextureFromSurface(renderer, bmp);
|
//texture = SDL_CreateTextureFromSurface(renderer, bmp);
|
||||||
//SDL_FreeSurface(bmp);
|
//SDL_FreeSurface(bmp);
|
||||||
@@ -130,6 +187,13 @@ void FillRect(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 a = 2
|
|||||||
SDL_RenderFillRect(sdlRenderer, &src);
|
SDL_RenderFillRect(sdlRenderer, &src);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawRect(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255) {
|
||||||
|
SDL_Rect src;
|
||||||
|
src.x = x; src.y = y; src.w = w; src.h = h;
|
||||||
|
SDL_SetRenderDrawColor(sdlRenderer, r, g, b, a);
|
||||||
|
SDL_RenderDrawRect(sdlRenderer, &src);
|
||||||
|
}
|
||||||
|
|
||||||
void Print(int x, int y, const char* text) {
|
void Print(int x, int y, const char* text) {
|
||||||
int str_size = strlen(text);
|
int str_size = strlen(text);
|
||||||
for (int i = 0; i < str_size; i++) {
|
for (int i = 0; i < str_size; i++) {
|
||||||
@@ -151,19 +215,29 @@ bool Button(int x, int y, char* text) {
|
|||||||
|
|
||||||
bool Update() {
|
bool Update() {
|
||||||
anyKey = false;
|
anyKey = false;
|
||||||
|
keyJustPressed = SDL_SCANCODE_UNKNOWN;
|
||||||
while (SDL_PollEvent(&sdlEvent)) {
|
while (SDL_PollEvent(&sdlEvent)) {
|
||||||
if (sdlEvent.type == SDL_QUIT) {return true; break; }
|
if (sdlEvent.type == SDL_QUIT) {return true; break; }
|
||||||
else if (sdlEvent.type == SDL_KEYDOWN) {
|
else if (sdlEvent.type == SDL_KEYDOWN) {
|
||||||
anyKey = true;
|
anyKey = true;
|
||||||
|
keyJustPressed = sdlEvent.key.keysym.scancode;
|
||||||
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { return true; }
|
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { return true; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Uint32 buttons = SDL_GetMouseState(&mouse.x, &mouse.y);
|
Uint32 buttons = SDL_GetMouseState(&mouse.x, &mouse.y);
|
||||||
mouse.x /= zoom;
|
mouse.x /= zoom;
|
||||||
mouse.y /= zoom;
|
mouse.y /= zoom;
|
||||||
mouse.buttons[0] = (buttons & 1) == 1;
|
|
||||||
mouse.buttons[1] = (buttons & 2) == 2;
|
if (buttons == 0) ignoreMouse = false;
|
||||||
mouse.buttons[2] = (buttons & 4) == 4;
|
|
||||||
|
if (buttons != 0 && !ignoreMouse) {
|
||||||
|
mouse.buttons[0] = (buttons & 1) == 1;
|
||||||
|
mouse.buttons[1] = (buttons & 2) == 2;
|
||||||
|
mouse.buttons[2] = (buttons & 4) == 4;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mouse.buttons[0] = mouse.buttons[1] = mouse.buttons[2] = false;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,10 +248,66 @@ void PrintFatalError(const char* errorMsg) {
|
|||||||
void ShowError() {
|
void ShowError() {
|
||||||
switch (error) {
|
switch (error) {
|
||||||
case 1: PrintFatalError("No s'ha trobat l'arxiu config.ini"); break;
|
case 1: PrintFatalError("No s'ha trobat l'arxiu config.ini"); break;
|
||||||
case 2: PrintFatalError("No s'ha trobat arxiu de tiles"); break;
|
case 2: PrintFatalError("No s'ha trobat l'arxiu de tiles"); break;
|
||||||
|
case 3: PrintFatalError("No s'ha trobat l'arxiu de mapa"); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DoTilePal() {
|
||||||
|
int ox = 0, oy = 0;
|
||||||
|
if (tilemap_width <= screen_width) {
|
||||||
|
ox = (screen_width - tilemap_width) / 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const int max_offset = tilemap_width - screen_width;
|
||||||
|
const float mouse_ratio = float(mouse.x) / float(screen_width);
|
||||||
|
ox = -(mouse_ratio * max_offset);
|
||||||
|
}
|
||||||
|
if (tilemap_height <= screen_height) {
|
||||||
|
oy = (screen_height - tilemap_height) / 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const int max_offset = tilemap_height - screen_height;
|
||||||
|
const float mouse_ratio = float(mouse.y) / float(screen_height);
|
||||||
|
oy = -(mouse_ratio * max_offset);
|
||||||
|
}
|
||||||
|
Draw(sdlTilesTexture, ox, oy, 0, 0, tilemap_width, tilemap_height);
|
||||||
|
int tiles_per_row = tilemap_width / tile_width;
|
||||||
|
DrawRect(ox + (tile_back % tiles_per_row)*tile_width - 1, oy + (tile_back / tiles_per_row)*tile_height - 1, tile_width + 2, tile_height + 2, 255, 64, 255);
|
||||||
|
DrawRect(ox + (tile_sel % tiles_per_row)*tile_width - 1, oy + (tile_sel / tiles_per_row)*tile_height - 1, tile_width + 2, tile_height + 2, 255, 255, 255);
|
||||||
|
if (mouse.buttons[2]) { tile_back = ((mouse.x - ox) / tile_width) + ((mouse.y - oy) / tile_height) * tiles_per_row; state = STATE_TILEMAP; ignoreMouse = true; }
|
||||||
|
if (mouse.buttons[0]) { tile_sel = ((mouse.x - ox) / tile_width) + ((mouse.y - oy) / tile_height) * tiles_per_row; state = STATE_TILEMAP; ignoreMouse = true; }
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_Q) state = STATE_TILEMAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DoTileMap() {
|
||||||
|
const int width_in_tiles = screen_width / tile_width;
|
||||||
|
const int height_in_tiles = screen_height / tile_height;
|
||||||
|
const int pal_width_in_tiles = tilemap_width / tile_width;
|
||||||
|
for (int y = 0; y < height_in_tiles; y++) {
|
||||||
|
for (int x = 0; x < width_in_tiles; x++) {
|
||||||
|
unsigned char tile = map[(map_x + x) + (map_y + y) * map_width];
|
||||||
|
Draw(sdlTilesTexture, x*tile_width, y*tile_height, (tile % pal_width_in_tiles)*tile_width, (tile / pal_width_in_tiles)*tile_height, tile_width, tile_height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const int cur_x = mouse.x / tile_width;
|
||||||
|
const int cur_y = mouse.y / tile_height;
|
||||||
|
DrawRect(cur_x*tile_width-1, cur_y*tile_height-1, tile_width + 2, tile_height + 2, 255, 255, 255, 128);
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_Q) state = STATE_TILEPAL;
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_RIGHT || keyJustPressed == SDL_SCANCODE_D) if (map_x < map_width - width_in_tiles) map_x += width_in_tiles;
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_LEFT || keyJustPressed == SDL_SCANCODE_A) if (map_x > 0) map_x -= width_in_tiles;
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_DOWN || keyJustPressed == SDL_SCANCODE_S) if (map_y < map_height - height_in_tiles) map_y += height_in_tiles;
|
||||||
|
if (keyJustPressed == SDL_SCANCODE_UP || keyJustPressed == SDL_SCANCODE_W) if (map_y > 0) map_y -= height_in_tiles;
|
||||||
|
if (mouse.buttons[2]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; }
|
||||||
|
if (mouse.buttons[0]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_sel; }
|
||||||
|
|
||||||
|
if (mouse.buttons[1]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; }
|
||||||
|
}
|
||||||
|
|
||||||
|
void DoMiniMap() {
|
||||||
|
Draw(sdlMinimapTexture, 0, 0, 0, 0, map_width, map_height);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
Init();
|
Init();
|
||||||
|
|
||||||
@@ -185,23 +315,12 @@ int main(int argc, char* argv[]) {
|
|||||||
Clear();
|
Clear();
|
||||||
|
|
||||||
if (error == 0) {
|
if (error == 0) {
|
||||||
int x = 0, y = 0;
|
switch (state) {
|
||||||
if (tilemap_width <= screen_width) {
|
case STATE_TILEMAP: DoTileMap(); break;
|
||||||
x = (screen_width - tilemap_width) / 2;
|
case STATE_TILEPAL: DoTilePal(); break;
|
||||||
} else {
|
case STATE_MINIMAP: DoMiniMap(); break;
|
||||||
const int max_offset = tilemap_width - screen_width;
|
};
|
||||||
const float mouse_ratio = float(mouse.x) / float(screen_width);
|
|
||||||
x = -(mouse_ratio * max_offset);
|
|
||||||
}
|
|
||||||
if (tilemap_height <= screen_height) {
|
|
||||||
y = (screen_height - tilemap_height) / 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const int max_offset = tilemap_height - screen_height;
|
|
||||||
const float mouse_ratio = float(mouse.y) / float(screen_height);
|
|
||||||
y = -(mouse_ratio * max_offset);
|
|
||||||
}
|
|
||||||
Draw(sdlTilesTexture, x, y, 0, 0, tilemap_width, tilemap_height);
|
|
||||||
/*char num[5];
|
/*char num[5];
|
||||||
sprintf(num, "%d", mouse.x);
|
sprintf(num, "%d", mouse.x);
|
||||||
Print(100, 100, num);
|
Print(100, 100, num);
|
||||||
|
|||||||
Reference in New Issue
Block a user