Feature complete

This commit is contained in:
2016-05-18 12:52:08 +02:00
parent c2e376b529
commit 038e0c1e79
4 changed files with 182 additions and 65 deletions

View File

@@ -4,6 +4,6 @@ map_width = 240
map_height = 238 map_height = 238
screen_width = 320 screen_width = 320
screen_height = 224 screen_height = 224
zoom = 2 zoom = 3
tiles = tiles.png tiles = tiles.png
map = map.map map = map.map

236
main.cpp
View File

@@ -10,6 +10,10 @@
#define STATE_TILEPAL 1 #define STATE_TILEPAL 1
#define STATE_MINIMAP 2 #define STATE_MINIMAP 2
#define MOUSE_BUTTON_LEFT 0
#define MOUSE_BUTTON_MIDDLE 1
#define MOUSE_BUTTON_RIGHT 2
struct Mouse { struct Mouse {
int x, y; int x, y;
bool buttons[3]; bool buttons[3];
@@ -31,7 +35,8 @@ bool anyKey = false;
LoopFun loop; LoopFun loop;
Mouse mouse; Mouse mouse;
int error = 0; int error = 0;
int state = STATE_MINIMAP; int dialog = 0;
int state = STATE_TILEMAP;
int tile_width = 16; int tile_width = 16;
int tile_height = 16; int tile_height = 16;
@@ -82,13 +87,26 @@ void LoadConfig() {
fclose(f); fclose(f);
} }
void UpdateMiniMap() {
const int tmtw = (tilemap_width / tile_width);
Uint32* 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));
free(pixels);
}
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);
//SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); SDL_SetRenderDrawBlendMode(sdlRenderer, SDL_BLENDMODE_BLEND);
SDL_RenderSetLogicalSize(sdlRenderer, screen_width, screen_height); SDL_RenderSetLogicalSize(sdlRenderer, screen_width, screen_height);
unsigned short w, h; unsigned short w, h;
@@ -136,23 +154,36 @@ void Init() {
stbi_image_free(buffer); stbi_image_free(buffer);
f = fopen(map_filename, "rb"); 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); map = (unsigned char*)malloc(map_width*map_height);
fread(map, map_width*map_height, 1, f); if (f) {
fclose(f); int old_map_width = 0, old_map_height = 0;
fread(&old_map_width, 1, 1, f);
fread(&old_map_height, 1, 1, f);
if (old_map_width == map_width && old_map_height == map_height) {
fread(map, map_width*map_height, 1, f);
} else {
unsigned char* old_map = (unsigned char*)malloc(old_map_width*old_map_height);
fread(old_map, old_map_width*old_map_height, 1, f);
for (int y = 0; y < map_height; y++) {
for (int x = 0; x < map_width; x++) {
map[x + y*map_width] = old_map[x + y*old_map_width];
}
}
dialog = 2;
}
fclose(f);
} else {
f = fopen(map_filename, "wb");
if (!f) { error = 3; return; }
fwrite(&map_width, 1, 1, f);
fwrite(&map_height, 1, 1, f);
fwrite(map, map_width*map_height, 1, f);
fclose(f);
dialog = 1;
}
sdlMinimapTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, map_width, map_height); sdlMinimapTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, map_width, map_height);
pixels = (Uint32*)malloc(map_width * map_height*sizeof(Uint32)); UpdateMiniMap();
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); keyboard = SDL_GetKeyboardState(nullptr);
//SDL_Surface* bmp = SDL_LoadBMP("assets.bmp"); //SDL_Surface* bmp = SDL_LoadBMP("assets.bmp");
@@ -202,13 +233,13 @@ void Print(int x, int y, const char* text) {
} }
bool Button(int x, int y, char* text) { bool Button(int x, int y, char* text) {
bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 51) && (mouse.y < y + 8); bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 55) && (mouse.y < y + 12);
if (inside) { if (inside) {
FillRect(x, y, 51, 9, 128, 64, 64, 255); FillRect(x, y, 55, 13, 128, 64, 64, 255);
} else { } else {
FillRect(x, y, 51, 9, 255, 64, 64, 255); FillRect(x, y, 55, 13, 255, 64, 64, 255);
} }
Print(x+2, y+2, text); Print(x+4, y+4, text);
return inside && mouse.buttons[0]; return inside && mouse.buttons[0];
} }
@@ -246,9 +277,30 @@ 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("config.ini not found"); break;
case 2: PrintFatalError("No s'ha trobat l'arxiu de tiles"); break; case 2: PrintFatalError("tiles file not found"); break;
case 3: PrintFatalError("No s'ha trobat l'arxiu de mapa"); break; case 3: PrintFatalError("cannot create map file"); break;
}
}
void ShowDialog() {
int size = 0;
switch (dialog) {
case 1:
size = strlen("Map file not found");
Print((screen_width - (size * 6)) / 2, (screen_height - 6) / 2, "Map file not found");
size = strlen("A new one has been created");
Print((screen_width - (size * 6)) / 2, ((screen_height - 6) / 2)+6, "A new one has been created");
if (Button((screen_width - 55) / 2, screen_height - 40, "Ok")) { dialog = 0; ignoreMouse = true; }
break;
case 2:
size = strlen("Map has been resized");
Print((screen_width - (size * 6)) / 2, (screen_height - 6) / 2, "Map has been resized");
size = strlen("using config.ini info");
Print((screen_width - (size * 6)) / 2, ((screen_height - 6) / 2) + 6, "using config.ini info");
if (Button((screen_width - 55) / 2, screen_height - 40, "Ok")) { dialog = 0; ignoreMouse = true; }
break;
//case 3: PrintFatalError("No s'ha trobat l'arxiu de mapa"); break;
} }
} }
@@ -280,6 +332,8 @@ void DoTilePal() {
} }
void DoTileMap() { void DoTileMap() {
static bool menu = false;
const int width_in_tiles = screen_width / tile_width; const int width_in_tiles = screen_width / tile_width;
const int height_in_tiles = screen_height / tile_height; const int height_in_tiles = screen_height / tile_height;
const int pal_width_in_tiles = tilemap_width / tile_width; const int pal_width_in_tiles = tilemap_width / tile_width;
@@ -289,19 +343,70 @@ void DoTileMap() {
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); 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_E) state = STATE_MINIMAP;
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]; } if (!menu) {
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_TAB) {
menu = true; return; }
if (keyJustPressed == SDL_SCANCODE_Q) state = STATE_TILEPAL;
if (keyJustPressed == SDL_SCANCODE_E) { state = STATE_MINIMAP; UpdateMiniMap(); }
if (keyJustPressed == SDL_SCANCODE_X) { int temp = tile_back; tile_back = tile_sel; tile_sel = temp; }
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[MOUSE_BUTTON_RIGHT]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; }
if (mouse.buttons[MOUSE_BUTTON_LEFT]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_sel; }
if (mouse.buttons[MOUSE_BUTTON_MIDDLE]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; }
} else {
if (keyJustPressed == SDL_SCANCODE_TAB) { menu = false; return; }
FillRect(0, 0, screen_width, screen_height, 0, 0, 0, 192);
if (Button(6, 6, "Save")) {
FILE* f = fopen(map_filename, "wb");
fwrite(&map_width, 1, 1, f);
fwrite(&map_height, 1, 1, f);
fwrite(map, map_width*map_height, 1, f);
fclose(f);
menu = false; ignoreMouse = true;
}
if (Button(6, 24, "Clear")) {
for (int y = 0; y < height_in_tiles; y++) {
for (int x = 0; x < width_in_tiles; x++) {
map[(map_x + x) + (map_y + y) * map_width] = tile_back;
}
}
menu = false; ignoreMouse = true;
}
if (Button(6, 42, "Border")) {
for (int y = 0; y < height_in_tiles; y++) {
map[(map_x) + (map_y + y) * map_width] = tile_sel;
map[(map_x + width_in_tiles - 1) + (map_y + y) * map_width] = tile_sel;
}
for (int x = 0; x < width_in_tiles; x++) {
map[(map_x + x) + (map_y) * map_width] = tile_sel;
map[(map_x + x) + (map_y + height_in_tiles - 1) * map_width] = tile_sel;
}
menu = false; ignoreMouse = true;
}
}
}
#define mod(x, y) (((x % y)+y)%y)
void ShiftMap(int sx, int sy) {
int test = mod(-1, 240);
sx *= (screen_width/tile_width);
sy *= (screen_height / tile_height);
unsigned char* new_map = (unsigned char*)malloc(map_width*map_height);
for (int y = 0; y < map_height; y++) {
for (int x = 0; x < map_width; x++) {
new_map[x + y*map_width] = map[x + y*map_width];
}
}
} }
void DoMiniMap() { void DoMiniMap() {
@@ -328,14 +433,19 @@ void DoMiniMap() {
const int screen_height_in_tiles = screen_height / tile_height; const int screen_height_in_tiles = screen_height / tile_height;
DrawRect((ox + map_x) - 1, (oy + map_y) - 1, screen_width_in_tiles + 2, screen_height_in_tiles + 2, 255, 255, 255); DrawRect((ox + map_x) - 1, (oy + map_y) - 1, screen_width_in_tiles + 2, screen_height_in_tiles + 2, 255, 255, 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); //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[0]) { if (mouse.buttons[MOUSE_BUTTON_LEFT]) {
map_x = int((mouse.x-ox) / screen_width_in_tiles) * screen_width_in_tiles; map_x = int((mouse.x-ox) / screen_width_in_tiles) * screen_width_in_tiles;
map_y = int((mouse.y - oy) / screen_height_in_tiles) * screen_height_in_tiles; map_y = int((mouse.y - oy) / screen_height_in_tiles) * screen_height_in_tiles;
state = STATE_TILEMAP; state = STATE_TILEMAP;
ignoreMouse = true; ignoreMouse = true;
} }
if (keyJustPressed == SDL_SCANCODE_E) state = STATE_TILEMAP; if (keyJustPressed == SDL_SCANCODE_E) state = STATE_TILEMAP;
if (keyboard[SDL_SCANCODE_LCTRL]) {
if (keyJustPressed == SDL_SCANCODE_A) ShiftMap(-1, 0);
if (keyJustPressed == SDL_SCANCODE_D) ShiftMap(1, 0);
if (keyJustPressed == SDL_SCANCODE_W) ShiftMap(0, -1);
if (keyJustPressed == SDL_SCANCODE_S) ShiftMap(0, 1);
}
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@@ -345,33 +455,37 @@ int main(int argc, char* argv[]) {
Clear(); Clear();
if (error == 0) { if (error == 0) {
switch (state) { if (dialog == 0) {
case STATE_TILEMAP: DoTileMap(); break; switch (state) {
case STATE_TILEPAL: DoTilePal(); break; case STATE_TILEMAP: DoTileMap(); break;
case STATE_MINIMAP: DoMiniMap(); break; case STATE_TILEPAL: DoTilePal(); break;
}; case STATE_MINIMAP: DoMiniMap(); break;
};
/*char num[5]; /*char num[5];
sprintf(num, "%d", mouse.x); sprintf(num, "%d", mouse.x);
Print(100, 100, num); Print(100, 100, num);
sprintf(num, ",%d", mouse.y); sprintf(num, ",%d", mouse.y);
Print(118, 100, num); Print(118, 100, num);
if (Button(2, 2, "New")) { if (Button(2, 2, "New")) {
strcpy(state, "New pressed"); strcpy(state, "New pressed");
} }
if (Button(2, 12, "Open")) { if (Button(2, 12, "Open")) {
strcpy(state, "open pressed"); strcpy(state, "open pressed");
} }
if (Button(2, 22, "Save")) { if (Button(2, 22, "Save")) {
strcpy(state, "save pressed"); strcpy(state, "save pressed");
} }
if (Button(2, 32, "Save As")) { if (Button(2, 32, "Save As")) {
strcpy(state, "Save As pressed"); strcpy(state, "Save As pressed");
} }
FillRect(0, 183, 256, 9, 128, 128, 128, 255); FillRect(0, 183, 256, 9, 128, 128, 128, 255);
Print(2, 185, state);*/ Print(2, 185, state);*/
} else {
ShowDialog();
}
} else { } else {
ShowError(); ShowError();
} }

BIN
map.map

Binary file not shown.

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
@@ -46,6 +46,8 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<IncludePath>C:\dev\lib\sdl2\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\dev\lib\sdl2\lib\x86;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
@@ -56,7 +58,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>SDL2.lib;SDL2main.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
@@ -72,10 +74,11 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>