-
22Characters/Gruss.txt
-
BINapp/Cave.png
-
BINapp/action/Cave.dab
-
BINapp/action/Cave.png
-
17app/action/Character.cpp
-
10app/action/Character.h
-
15app/action/Level.h
-
4app/action/Makefile
-
190app/action/Map.cpp
-
66app/action/Map.h
-
11app/action/Texture.cpp
-
BINapp/action/action
-
BINapp/action/assets/0.png
-
BINapp/action/assets/1.png
-
BINapp/action/assets/2.png
-
BINapp/action/assets/Cave.bmp
-
BINapp/action/assets/Cave.dab
-
20app/action/main.cpp
-
16app/action/test.cpp
-
BINapp/action/treetest
-
BINapp/procgen/Cave.bmp
-
BINapp/procgen/Cave.dab
-
BINapp/procgen/Cave.png
-
20app/procgen/cave.py
After Width: 1920 | Height: 1080 | Size: 114 KiB |
Before Width: 1920 | Height: 1080 | Size: 114 KiB After Width: 1920 | Height: 1080 | Size: 116 KiB |
@ -1,15 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include "Texture.h" |
|||
|
|||
//very unfinished, do not use |
|||
|
|||
class Level { |
|||
private: |
|||
Texture texture_; |
|||
//list of players? |
|||
//list of monsters? |
|||
SDL_Rect camera; |
|||
public: |
|||
|
|||
}; |
@ -1,7 +1,7 @@ |
|||
CC = clang++ |
|||
FLAGS = -lSDL2 -lSDL2_image -lSDL2_gfx |
|||
EXEC = action |
|||
SRC = main.cpp Texture.cpp Character.cpp Timer.cpp |
|||
SRC = main.cpp Texture.cpp Character.cpp Timer.cpp Map.cpp |
|||
|
|||
all: main.cpp Texture.cpp Character.cpp Timer.cpp |
|||
all: $(SRC) |
|||
$(CC) $(FLAGS) -o $(EXEC) $(SRC) |
@ -0,0 +1,190 @@ |
|||
#include "Map.h"
|
|||
|
|||
Tile tileIndex(int b, int g, int r) { |
|||
if(b == 255 && g == 255 && r == 255) { |
|||
return GRASS; |
|||
} |
|||
return WALL; |
|||
} |
|||
|
|||
Quad::Quad() { |
|||
init(L_W,L_H); |
|||
} |
|||
|
|||
void Quad::init(int w,int h) { |
|||
int i = 1; |
|||
while(i < w || i < h) |
|||
i <<= 1; |
|||
bound = {0,0,i,i}; |
|||
data = WALL; |
|||
tl = tr = bl = br = NULL; |
|||
} |
|||
|
|||
void Quad::boundstring() { |
|||
std::cout << "{" << bound.x << "," << bound.y << "," << bound.w << "," << bound.h << "}\n"; |
|||
} |
|||
|
|||
void Quad::free() { |
|||
if(bound.w*bound.h == 1) { |
|||
delete this; |
|||
return; |
|||
} |
|||
if(tl) |
|||
tl->free(); |
|||
tl = NULL; |
|||
if(tr) |
|||
tr->free(); |
|||
tr = NULL; |
|||
if(bl) |
|||
bl->free(); |
|||
bl = NULL; |
|||
if(br) |
|||
br->free(); |
|||
br = NULL; |
|||
} |
|||
|
|||
inline bool Quad::inbounds(int x, int y) { |
|||
return (x - bound.x >= 0) && |
|||
(y - bound.y >= 0) && |
|||
(x-bound.x < bound.w) && |
|||
(y-bound.y < bound.h); |
|||
} |
|||
|
|||
void Quad::insert(int x, int y, Tile t) { |
|||
//std::cout << "Insert called at " << x << "," << y << " on " << this << "\n";
|
|||
if(!inbounds(x,y)) { |
|||
std::cout << "Not inbounds\n"; |
|||
return; |
|||
} |
|||
if(bound.w*bound.h == 1) { |
|||
data = t; |
|||
//std::cout <<"Data is set to" << t << "\n";
|
|||
return; |
|||
} |
|||
if(x-bound.x < bound.w/2) { //left
|
|||
if(y - bound.y < bound.h/2) { //top
|
|||
if(!tl) { |
|||
tl = new Quad(); |
|||
tl->bound = {bound.x,bound.y,bound.w/2,bound.h/2}; |
|||
} |
|||
tl->insert(x,y,t); |
|||
} else { //bottom
|
|||
if(!bl) { |
|||
bl = new Quad(); |
|||
bl->bound = {bound.x,bound.y+bound.h/2,bound.w/2,bound.h/2}; |
|||
} |
|||
bl->insert(x,y,t); |
|||
} |
|||
} else { //right
|
|||
if(y - bound.y < bound.h/2) { //top
|
|||
if(!tr) { |
|||
tr = new Quad(); |
|||
tr->bound = {bound.x+bound.w/2,bound.y,bound.w/2,bound.h/2}; |
|||
} |
|||
tr->insert(x,y,t); |
|||
} else { //bottom
|
|||
if(!br) { |
|||
br = new Quad(); |
|||
br->bound = {bound.x+bound.w/2,bound.y+bound.h/2,bound.w/2,bound.h/2}; |
|||
} |
|||
br->insert(x,y,t); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Tile Quad::lookup(int x, int y) { |
|||
if(bound.w*bound.h == 1) { |
|||
return data; |
|||
} |
|||
if(x - bound.x < bound.w/2) { |
|||
if(y - bound.y < bound.h/2) { |
|||
if(tl) |
|||
return tl->lookup(x,y); |
|||
return WALL; |
|||
} |
|||
if(tr) |
|||
return tr->lookup(x,y); |
|||
return WALL; |
|||
} else { |
|||
if(y - bound.y < bound.h/2) { |
|||
if(bl) |
|||
return bl->lookup(x,y); |
|||
return WALL; |
|||
} |
|||
if(br) |
|||
return br->lookup(x,y); |
|||
return WALL; |
|||
} |
|||
} |
|||
|
|||
void Quad::print(int spaces) { |
|||
for(int i = 0; i < spaces; ++i) { |
|||
std::cout << " "; |
|||
} |
|||
if(data == WALL) |
|||
std::cout << "WALL\n"; |
|||
if(data == STONE) |
|||
std::cout << "STONE\n"; |
|||
if(data == GRASS) |
|||
std::cout << "GRASS\n"; |
|||
if(tl) |
|||
tl->print(spaces+2); |
|||
if(tr) |
|||
tr->print(spaces+2); |
|||
if(bl) |
|||
bl->print(spaces+2); |
|||
if(br) |
|||
br->print(spaces+2); |
|||
} |
|||
|
|||
Map::Map() { |
|||
tiles = new Quad(); |
|||
numTilesX = numTilesY = -1; |
|||
|
|||
for(int i = 0; i < NUM_TILE_IDs; ++i) { |
|||
std::string filename = "assets/" + std::to_string(i) + ".png"; |
|||
std::cout << "Loading texture " << filename << "\n"; |
|||
textures[i].loadFromFile(filename); |
|||
} |
|||
|
|||
} |
|||
|
|||
Map::~Map() { |
|||
tiles->free(); |
|||
delete tiles; //This destructor may seem excessive, but all three of these lines have a separate purpose.
|
|||
tiles = NULL; //c'est la C++
|
|||
} |
|||
|
|||
void Map::loadFromFile(std::string fpath) { |
|||
FILE* f = fopen(fpath.c_str(),"rb"); |
|||
int w,h; |
|||
fread(&w,sizeof(int),1,f); |
|||
fread(&h,sizeof(int),1,f); |
|||
std::cout << "Width: " << w << " Height: " << h << "\n"; |
|||
tiles->init(w,h); |
|||
std::cout << "Tiles init'd\n"; |
|||
int t; |
|||
for(int i = 0; i < w*h; ++i) { |
|||
fread(&t,sizeof(int),1,f); |
|||
tiles->insert(i/w,i%w,static_cast<Tile>(t)); |
|||
//std::cout << "Loading " << t << "\n";
|
|||
} |
|||
fclose(f); |
|||
numTilesX = w; |
|||
numTilesY = h; |
|||
} |
|||
|
|||
void Map::render(SDL_Rect camera) { |
|||
std::cout << "Rendering map\n"; |
|||
for(int x = camera.x/TILE_SIZE; x < 1+(camera.x + camera.w)/TILE_SIZE; ++x){ |
|||
for(int y = camera.y/TILE_SIZE; y < 1+(camera.y + camera.h)/TILE_SIZE; ++y) { |
|||
Tile t = tiles->lookup(x,y); |
|||
//std::cout << "Tile at " << x << "," << y << " is " << t << "\n";
|
|||
textures[t].render(x*TILE_SIZE - camera.x,y*TILE_SIZE - camera.y); |
|||
} |
|||
} |
|||
} |
|||
|
|||
void Map::print() { |
|||
tiles->print(0); |
|||
} |
@ -0,0 +1,66 @@ |
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <iostream> |
|||
|
|||
#include <SDL2/SDL.h> |
|||
|
|||
#include "Texture.h" |
|||
|
|||
|
|||
extern const int S_W; //screen dimensions |
|||
extern const int S_H; |
|||
|
|||
extern int L_W; //level dimensions |
|||
extern int L_H; |
|||
|
|||
extern const int TILE_SIZE; |
|||
static const int NUM_TILE_IDs = 3; |
|||
|
|||
enum Tile { |
|||
WALL, |
|||
STONE, |
|||
GRASS |
|||
}; |
|||
|
|||
class Quad { |
|||
private: |
|||
SDL_Rect bound; |
|||
Tile data; |
|||
inline bool inbounds(int,int); |
|||
void boundstring(); |
|||
Quad *tl,*tr,*bl,*br; |
|||
public: |
|||
Quad(); |
|||
void init(int,int); |
|||
//~Quad(){free();} |
|||
void free(); |
|||
void insert(int,int,Tile); |
|||
Tile lookup(int,int); //defaults to wall |
|||
void print(int); |
|||
}; |
|||
|
|||
class Map { |
|||
|
|||
private: |
|||
int numTilesX,numTilesY; |
|||
Quad* tiles; |
|||
|
|||
Texture textures[NUM_TILE_IDs]; |
|||
|
|||
public: |
|||
|
|||
Map(); |
|||
~Map(); |
|||
|
|||
void loadFromFile(std::string fpath); |
|||
|
|||
void render(SDL_Rect camera); |
|||
|
|||
//DEBUG |
|||
void print(); |
|||
|
|||
int getW(){ return numTilesX; } |
|||
int getH(){ return numTilesY; } |
|||
|
|||
}; |
After Width: 64 | Height: 64 | Size: 220 B |
After Width: 64 | Height: 64 | Size: 1.9 KiB |
After Width: 64 | Height: 64 | Size: 5.1 KiB |
@ -0,0 +1,16 @@ |
|||
#include "Map.h"
|
|||
|
|||
const int S_W = 640; |
|||
const int S_H = 480; |
|||
|
|||
const int L_W = 1920; |
|||
const int L_H = 1080; |
|||
|
|||
int main() { |
|||
|
|||
Map m; |
|||
m.loadFromFile("assets/Cave.bmp"); |
|||
m.print(); |
|||
|
|||
return 0; |
|||
} |
Before Width: 1920 | Height: 1080 | Size: 114 KiB |