Skip to content

Commit

Permalink
give map to gamestate
Browse files Browse the repository at this point in the history
  • Loading branch information
g4stly committed Sep 25, 2019
1 parent 5a55bfa commit 24d3608
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LIBS = -lSDL2
TARGET = build/raycast
SOURCES = src/app.cpp
SOURCES += src/display/display.cpp src/display/engine.cpp
SOURCES += src/game/gamestate.cpp
SOURCES += src/game/gamestate.cpp src/game/map.cpp
SOURCES += src/util/vector.cpp
OBJECTS = $(SOURCES:.cpp=.o)

Expand Down
32 changes: 7 additions & 25 deletions src/display/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@

#include "engine.h"

const static int mock_map[20*20] = {
1, 8, 1, 8, 8, 8, 1, 8, 1, 1, 8, 1, 8, 8, 8, 1, 8, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 4,
4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
6, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 4,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
6, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 4, 4, 1, 4, 1, 4, 1, 1, 1, 4, 4, 1, 4, 1, 4, 1, 1
};

struct RayCastArgs {
// arguments
const int *map;
Expand Down Expand Up @@ -118,7 +95,10 @@ static void rayCaster(RayCastArgs *args)
}

for (int y = a.line_height; y >= 0; y--) {
a.color = a.hit > 0 ? 0xFFFFFFFF : 0xFFD0D0D0;
a.color = 0xFF0000;
if (a.hit < 0) {
a.color = (a.color << 1) & 0x7F7F7F;
}
a.pixels[x + (a.screenw * (a.line_start - y))] = a.color;
}
}
Expand All @@ -130,7 +110,9 @@ void Engine::Render(GameState *g, int w, int h, uint32_t *pixels, int pitch)

// real position, direction, and camera plane vectors
RayCastArgs args = {
mock_map, 19, 19, w, h,
g->GetMap(),
g->GetMapWidth() - 1,
g->GetMapHeight() - 1, w, h,
g->GetPosX(), g->GetPosY(),
g->GetDirX(), g->GetDirY(),
g->GetPlaneX(), g->GetPlaneY(),
Expand Down
16 changes: 16 additions & 0 deletions src/game/gamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,19 @@ void GameState::Rotate(bool left, double dt)
direction.Rotate(theta);
camera.Rotate(theta);
}

int GameState::GetMapWidth() {
return map.width;
}

int GameState::GetMapHeight() {
return map.height;
}

const int *GameState::GetMap() {
return map.map;
}

const uint32_t *GameState::GetTextures() {
return map.textures;
}
11 changes: 11 additions & 0 deletions src/game/gamestate.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef gamestate_h_
#define gamestate_h_

#include "map.h"
#include "util/vector.h"

#define MAP_WIDTH (10)
Expand All @@ -20,6 +21,9 @@ class GameState {
Vector camera;
double rot_speed = 3.0;
double move_speed = 5.0;

// map
Map map;
public:
GameState();

Expand All @@ -31,6 +35,7 @@ class GameState {
private:
void Move(Vector *v, bool forward, double dt);
public:
// player methods
double GetPosX();
double GetPosY();
double GetDirX();
Expand All @@ -40,6 +45,12 @@ class GameState {
void Move(bool forward, double dt);
void Strafe(bool left, double dt);
void Rotate(bool left, double dt);

// map methods
int GetMapWidth();
int GetMapHeight();
const int *GetMap();
const uint32_t *GetTextures();
};


Expand Down
32 changes: 32 additions & 0 deletions src/game/map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "map.h"

#define MAP_WIDTH (20)
#define MAP_HEIGHT (20)
static int mock_map[MAP_WIDTH*MAP_HEIGHT] = {
1, 8, 1, 8, 8, 8, 1, 8, 1, 1, 8, 1, 8, 8, 8, 1, 8, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 4,
4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
6, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 4,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
6, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 4, 4, 1, 4, 1, 4, 1, 1, 1, 4, 4, 1, 4, 1, 4, 1, 1
};

Map::Map() {
width = MAP_WIDTH;
height = MAP_HEIGHT;
map = mock_map;
}
14 changes: 14 additions & 0 deletions src/game/map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef MAP_H_
#define MAP_H_

#include <cstdint>

class Map {
public:
int *map;
int width, height;
uint32_t textures[4096];
Map();
};

#endif

0 comments on commit 24d3608

Please sign in to comment.