This commit is contained in:
Tobias Berger 2021-11-30 16:41:03 +01:00
parent 95f105fd96
commit ce5426cef5
14 changed files with 392 additions and 42 deletions

View file

@ -7,7 +7,7 @@
"${workspaceFolder}/raylib/src/extras" "${workspaceFolder}/raylib/src/extras"
], ],
"windowsSdkVersion": "10.0.19041.0", "windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/mingw/mingw32/bin/gcc.exe", "compilerPath": "D:/mingw/mingw64/bin/gcc.exe",
"cStandard": "c17", "cStandard": "c17",
"cppStandard": "c++17", "cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64", "intelliSenseMode": "windows-gcc-x64",

View file

@ -15,7 +15,8 @@
"*.m": "cpp", "*.m": "cpp",
"cstdlib": "cpp", "cstdlib": "cpp",
"raylib.h": "c", "raylib.h": "c",
"math.h": "c" "math.h": "c",
"raymath.h": "c"
} }
} }
} }

View file

@ -1,5 +1,10 @@
#include "raylib.h" #include "raylib.h"
static Color const ERROR_COLOR = {255, 0, 255, 255};
static Color const FLOOR_COLOR = {255, 255, 255, 255};
static Color const WALL_COLOR = {0, 0, 0, 255};
static Color const PLAYER_COLOR = {64, 64, 64 + 128, 255};
typedef struct Vector2i typedef struct Vector2i
{ {
int x; int x;
@ -12,20 +17,36 @@ typedef struct Entity
Color color; Color color;
} Entity; } Entity;
typedef struct Room
{
int height;
int width;
Vector2i position;
Vector2i center;
} Room;
typedef struct Tile typedef struct Tile
{ {
bool walkable; bool walkable;
Color color; Color color;
} Tile; } Tile;
Entity *createPlayer(Vector2i start_position); bool tryMovePlayer(Vector2i const new_position);
Tile **createMapTiles();
void freeMapTiles(Tile **map_tiles);
void renderEverything(RenderTexture2D target);
void initGame(); Entity *createPlayer(Vector2i const start_position);
void mainLoop(); Tile **createMapTiles(void);
void endGame(); Vector2i setupMap(void);
void freeMapTiles(Tile **map_tiles);
Room createRoom(int x, int y, int height, int width);
void addRoomToMap(Room room);
void connectRoomCenters(Vector2i centerOne, Vector2i centerTwo);
void renderEverything(void);
void initGame(void);
void mainLoop(void);
void endGame(void);
extern unsigned int const MAP_HEIGHT; extern unsigned int const MAP_HEIGHT;
extern unsigned int const MAP_WIDTH; extern unsigned int const MAP_WIDTH;

Binary file not shown.

BIN
resources/TINY.TTF Normal file

Binary file not shown.

BIN
resources/bmfont64.exe Normal file

Binary file not shown.

100
resources/tiny.fnt Normal file
View file

@ -0,0 +1,100 @@
info face="Tiny" size=-6 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=0 aa=1 padding=1,1,0,1 spacing=0,0 outline=0
common lineHeight=6 base=5 scaleW=128 scaleH=128 pages=1 packed=0 alphaChnl=0 redChnl=3 greenChnl=3 blueChnl=3
page id=0 file="tiny_0.png"
chars count=96
char id=32 x=0 y=28 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=33 x=37 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=34 x=7 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=35 x=16 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=36 x=13 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=37 x=70 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=38 x=77 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=39 x=25 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=40 x=118 y=21 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=41 x=113 y=21 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=42 x=19 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=43 x=25 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=44 x=41 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=45 x=37 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=46 x=57 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=47 x=119 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=48 x=43 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=49 x=49 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=50 x=55 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=51 x=61 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=52 x=67 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=53 x=73 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=54 x=79 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=55 x=85 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=56 x=102 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=57 x=91 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=58 x=33 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=59 x=29 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=60 x=97 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=61 x=103 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=62 x=109 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=63 x=115 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=64 x=121 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=65 x=0 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=66 x=6 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=67 x=12 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=68 x=31 y=7 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=69 x=18 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=70 x=24 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=71 x=30 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=72 x=36 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=73 x=42 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=74 x=48 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=75 x=98 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=76 x=54 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=77 x=0 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=78 x=91 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=79 x=105 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=80 x=90 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=81 x=63 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=82 x=66 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=83 x=72 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=84 x=78 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=85 x=56 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=86 x=48 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=87 x=40 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=88 x=84 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=89 x=32 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=90 x=84 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=91 x=5 y=28 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=92 x=112 y=0 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=93 x=15 y=28 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=94 x=96 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=95 x=102 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=96 x=108 y=21 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=97 x=108 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=98 x=114 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=99 x=120 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=100 x=0 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=101 x=6 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=102 x=10 y=28 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=103 x=20 y=28 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=104 x=18 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=105 x=45 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=106 x=49 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=107 x=24 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=108 x=61 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=109 x=24 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=110 x=30 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=111 x=36 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=112 x=42 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=113 x=48 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=114 x=123 y=21 width=5 height=7 xoffset=-1 yoffset=-1 xadvance=3 page=0 chnl=15
char id=115 x=12 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=116 x=54 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=117 x=60 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=118 x=66 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=119 x=8 y=0 width=8 height=7 xoffset=-1 yoffset=-1 xadvance=6 page=0 chnl=15
char id=120 x=72 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=121 x=78 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=122 x=84 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=123 x=90 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=124 x=53 y=28 width=4 height=7 xoffset=-1 yoffset=-1 xadvance=2 page=0 chnl=15
char id=125 x=96 y=21 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15
char id=126 x=0 y=7 width=7 height=7 xoffset=-1 yoffset=-1 xadvance=5 page=0 chnl=15
char id=127 x=60 y=14 width=6 height=7 xoffset=-1 yoffset=-1 xadvance=4 page=0 chnl=15

BIN
resources/tiny_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

View file

@ -17,12 +17,8 @@ void drawEntity(Entity *entity)
DrawPixel(entity->position.x, entity->position.y, entity->color); DrawPixel(entity->position.x, entity->position.y, entity->color);
} }
void renderEverything(RenderTexture2D target) void renderEverything()
{ {
BeginTextureMode(target);
{
drawMap(map); drawMap(map);
drawEntity(player); drawEntity(player);
}
EndTextureMode();
} }

View file

@ -1,5 +1,6 @@
#include "raylib.h" #include "raylib.h"
#include "rogue.h" #include "rogue.h"
#include <time.h> // Required for: time_t, tm, time(), localtime(), strftime()
static int const PIXEL_SCALE = 10; static int const PIXEL_SCALE = 10;
static int const VIRTUAL_SCREEN_WIDTH = 80; static int const VIRTUAL_SCREEN_WIDTH = 80;
@ -22,16 +23,19 @@ void initGame()
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
#if defined(__DEBUG) #if defined(__DEBUG)
SetTraceLogLevel(LOG_ALL); SetTraceLogLevel(LOG_ALL);
SetRandomSeed(0);
#else #else
SetTraceLogLevel(LOG_NONE); SetTraceLogLevel(LOG_NONE);
SetRandomSeed(time(NULL));
#endif #endif
SetConfigFlags(0); SetConfigFlags(0);
InitWindow(REAL_SCREEN_WIDTH, REAL_SCREEN_HEIGHT, "TODO: Add a title"); InitWindow(REAL_SCREEN_WIDTH, REAL_SCREEN_HEIGHT, "TODO: Add a title");
virtualScreen = LoadRenderTexture(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT); virtualScreen = LoadRenderTexture(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
player = createPlayer((Vector2i){VIRTUAL_SCREEN_WIDTH / 2, VIRTUAL_SCREEN_HEIGHT / 2});
map = createMapTiles(); map = createMapTiles();
player = createPlayer(setupMap());
// SetTargetFPS(60); // Set our game to run at 60 frames-per-second // SetTargetFPS(60); // Set our game to run at 60 frames-per-second
} }
@ -41,22 +45,24 @@ void update()
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
Vector2i new_position = {player->position.x, player->position.y};
if (IsKeyPressed(KEY_RIGHT)) if (IsKeyPressed(KEY_RIGHT))
{ {
player->position.x++; new_position.x++;
} }
if (IsKeyPressed(KEY_LEFT)) if (IsKeyPressed(KEY_LEFT))
{ {
player->position.x--; new_position.x--;
} }
if (IsKeyPressed(KEY_UP)) if (IsKeyPressed(KEY_UP))
{ {
player->position.y--; new_position.y--;
} }
if (IsKeyPressed(KEY_DOWN)) if (IsKeyPressed(KEY_DOWN))
{ {
player->position.y++; new_position.y++;
} }
tryMovePlayer(new_position);
} }
void draw() void draw()
@ -65,30 +71,14 @@ void draw()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginTextureMode(virtualScreen); BeginTextureMode(virtualScreen);
{ {
ClearBackground(BLACK); ClearBackground(ERROR_COLOR);
DrawPixel(player->position.x, player->position.y, player->color); renderEverything();
/*
for (unsigned int x = 0; x < VIRTUAL_SCREEN_WIDTH; x++)
{
for (unsigned int y = 0; y < VIRTUAL_SCREEN_HEIGHT; y++)
{
const unsigned char r = (unsigned char)floorf((float)x / (float)VIRTUAL_SCREEN_WIDTH * 255.0f);
const unsigned char b = (unsigned char)floorf((float)y / (float)VIRTUAL_SCREEN_HEIGHT * 255.0f);
const unsigned char g = 255 - (r + b) / 2;
const unsigned char a = 255;
DrawPixel(x, y, (Color){r, g, b, a});
}
}
*/
} }
EndTextureMode(); EndTextureMode();
BeginDrawing(); BeginDrawing();
{ {
ClearBackground(BLACK);
// Draw rendered game texture // Draw rendered game texture
DrawTexturePro(virtualScreen.texture, virtualScreenRect, realScreenRect, (Vector2){0.0f, 0.0f}, 0.0f, WHITE); DrawTexturePro(virtualScreen.texture, virtualScreenRect, realScreenRect, (Vector2){0.0f, 0.0f}, 0.0f, WHITE);

View file

@ -7,6 +7,136 @@ Tile **map;
int main(void) int main(void)
{ {
char chars[] = {
' ',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
':',
';',
'<',
'=',
'>',
'?',
'@',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'[',
'\\',
']',
'^',
'_',
'`',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'{',
'|',
'}',
'~',
127,
};
InitWindow(800, 600, "Font test");
Font tiny = LoadFont("resources/tiny.png");
RenderTexture2D target = LoadRenderTexture(80, 60);
static const int font_size = 6;
float const charWidth = MeasureTextEx(tiny, " ", font_size, 0).x;
BeginTextureMode(target);
ClearBackground(BLACK);
for (unsigned char i = 0; i < 96; i++)
{
float const x = i * charWidth;
float const y = ((int)x / target.texture.width) * font_size;
char const *const text = TextFormat("%c", chars[i]);
if (y != 0)
{
TraceLog(LOG_INFO, TextFormat("i: %i, x: %f, y: %f", i, x, y));
}
DrawTextEx(tiny, text, (Vector2){x, y}, font_size, 2, WHITE);
}
EndTextureMode();
while (!WindowShouldClose())
{
BeginDrawing();
DrawTexturePro(target.texture, (Rectangle){0, 0, 80, -60}, (Rectangle){0, 0, 800, 600}, (Vector2){0, 0}, 0, WHITE);
EndDrawing();
}
CloseWindow();
return 0;
/*
initGame(); initGame();
// Main game loop // Main game loop
@ -18,4 +148,5 @@ int main(void)
endGame(); endGame();
return 0; return 0;
*/
} }

View file

@ -1,6 +1,6 @@
#include "rogue.h" #include "rogue.h"
Tile **createMapTiles() Tile **createMapTiles(void)
{ {
Tile **tiles = MemAlloc(sizeof(Tile *) * MAP_WIDTH); Tile **tiles = MemAlloc(sizeof(Tile *) * MAP_WIDTH);
@ -9,7 +9,7 @@ Tile **createMapTiles()
tiles[x] = MemAlloc(sizeof(Tile) * MAP_HEIGHT); tiles[x] = MemAlloc(sizeof(Tile) * MAP_HEIGHT);
for (unsigned int y = 0; y < MAP_HEIGHT; y++) for (unsigned int y = 0; y < MAP_HEIGHT; y++)
{ {
tiles[x][y].color = WHITE; tiles[x][y].color = WALL_COLOR;
tiles[x][y].walkable = false; tiles[x][y].walkable = false;
} }
} }
@ -17,6 +17,44 @@ Tile **createMapTiles()
return tiles; return tiles;
} }
static const unsigned int MIN_ROOM_WIDTH = 5;
static const unsigned int MIN_ROOM_HEIGHT = 3;
static const unsigned int MAX_ROOM_WIDTH = 20;
static const unsigned int MAX_ROOM_HEIGHT = 10;
Vector2i setupMap(void)
{
int y, x, height, width, n_rooms;
n_rooms = GetRandomValue(5, 15);
Vector2i start_pos;
Room *const rooms = MemAlloc(n_rooms * sizeof(Room));
for (int i = 0; i < n_rooms; i++)
{
x = GetRandomValue(1, MAP_WIDTH - MAX_ROOM_WIDTH - 1U);
y = GetRandomValue(1, MAP_HEIGHT - MAX_ROOM_HEIGHT - 1U);
width = GetRandomValue(MIN_ROOM_WIDTH, MAX_ROOM_WIDTH);
height = GetRandomValue(MIN_ROOM_HEIGHT, MAX_ROOM_HEIGHT);
rooms[i] = createRoom(x, y, width, height);
addRoomToMap(rooms[i]);
if (i > 0)
{
connectRoomCenters(rooms[i - 1].center, rooms[i].center);
}
}
start_pos = (Vector2i){rooms[0].center.x, rooms[0].center.y};
MemFree(rooms);
return start_pos;
}
void freeMapTiles(Tile **map_tiles) void freeMapTiles(Tile **map_tiles)
{ {
for (unsigned int x = 0; x < MAP_WIDTH; x++) for (unsigned int x = 0; x < MAP_WIDTH; x++)

View file

@ -6,7 +6,17 @@ Entity *createPlayer(Vector2i const start_position)
Entity *const newPlayer = MemAlloc(sizeof(Entity)); Entity *const newPlayer = MemAlloc(sizeof(Entity));
newPlayer->position = start_position; newPlayer->position = start_position;
newPlayer->color = DARKBLUE; newPlayer->color = PLAYER_COLOR;
return newPlayer; return newPlayer;
} }
bool tryMovePlayer(Vector2i const new_position)
{
if (map[new_position.x][new_position.y].walkable)
{
player->position = new_position;
return true;
}
return false;
}

63
src/room.c Normal file
View file

@ -0,0 +1,63 @@
#include "rogue.h"
#include "raymath.h"
Room createRoom(int x, int y, int width, int height)
{
Room newRoom = {
.position = {x, y},
.height = height,
.width = width,
.center = {
x + (int)(width / 2),
y + (int)(height / 2),
},
};
return newRoom;
}
void addRoomToMap(Room room)
{
for (int x = room.position.x; x < room.position.x + room.width; x++)
{
for (int y = room.position.y; y < room.position.y + room.height; y++)
{
map[x][y].color = FLOOR_COLOR;
map[x][y].walkable = true;
}
}
}
void connectRoomCenters(Vector2i center1, Vector2i center2)
{
Vector2i current = {
center1.x,
center1.y,
};
while (true)
{
if (abs((current.x - 1) - center2.x) < abs(current.x - center2.x))
{
current.x--;
}
else if (abs((current.x + 1) - center2.x) < abs(current.x - center2.x))
{
current.x++;
}
else if (abs((current.y - 1) - center2.y) < abs(current.y - center2.y))
{
current.y--;
}
else if (abs((current.y + 1) - center2.y) < abs(current.y - center2.y))
{
current.y++;
}
else
{
break;
}
map[current.x][current.y].color = FLOOR_COLOR;
map[current.x][current.y].walkable = true;
}
}