Browse Source

Fix image loading and quad tree bug

master
Josh Gordon 4 years ago
parent
commit
c3836888d4
  1. BIN
      app/action/Cave.dab
  2. BIN
      app/action/Cave.png
  3. 10
      app/action/Character.cpp
  4. 2
      app/action/Character.h
  5. 2
      app/action/Makefile
  6. 67
      app/action/Map.cpp
  7. 8
      app/action/Map.h
  8. BIN
      app/action/action
  9. BIN
      app/action/assets/Cave.dab
  10. BIN
      app/action/assets/Cave.png
  11. BIN
      app/action/assets/test.png
  12. BIN
      app/action/assets/test2.png
  13. BIN
      app/action/assets/test3.png
  14. 33
      app/action/main.cpp
  15. 18
      app/action/test.cpp
  16. BIN
      app/action/treetest
  17. BIN
      app/procgen/Cave.png
  18. 2
      app/procgen/cave.py

BIN
app/action/Cave.dab

BIN
app/action/Cave.png

Before

Width: 1920  |  Height: 1080  |  Size: 116 KiB

10
app/action/Character.cpp

@ -20,15 +20,19 @@ void Character::handleEvent(SDL_Event& e) {
//update function
void Character::move() {
printf("Moving char at (%d,%d)\n",x_,y_);
printf("Moving char at (%d,%d)\n",x_/TILE_SIZE,y_/TILE_SIZE);
x_ += vx_;
if((x_ < 0) || (x_ + sprite_.getWidth() > L_W*TILE_SIZE))
if((x_ < 0) || (x_ + sprite_.getWidth() > L_W*TILE_SIZE)) {
printf("%d is too far on x, bounds are 0 and %d\n",x_,L_W*TILE_SIZE);
x_ -= vx_;
}
y_ += vy_;
if((y_ < 0) || (y_ + sprite_.getHeight() > L_H*TILE_SIZE))
if((y_ < 0) || (y_ + sprite_.getHeight() > L_H*TILE_SIZE)) {
printf("%d is too far on y, bounds are 0 and %d\n",y_,L_H*TILE_SIZE);
y_ -= vy_;
}
//camera time

2
app/action/Character.h

@ -21,7 +21,7 @@ private:
SDL_Rect camera_;
public:
static const int CHAR_SPEED = 10;
static const int CHAR_SPEED = 250;
Character(std::string filepath, int x, int y): x_(x), y_(y), vx_(0), vy_(0), camera_({0,0,S_W,S_H}) {
sprite_.loadFromFile(filepath);

2
app/action/Makefile

@ -1,5 +1,5 @@
CC = clang++
FLAGS = -lSDL2 -lSDL2_image -lSDL2_gfx
FLAGS = -lSDL2 -lSDL2_image -lSDL2_gfx -lopencv_core -lopencv_highgui -lopencv_imgcodecs
EXEC = action
SRC = main.cpp Texture.cpp Character.cpp Timer.cpp Map.cpp

67
app/action/Map.cpp

@ -1,5 +1,6 @@
#include "Map.h"
Tile tileIndex(int b, int g, int r) {
if(b == 255 && g == 255 && r == 255) {
return GRASS;
@ -25,10 +26,10 @@ void Quad::boundstring() {
}
void Quad::free() {
if(bound.w*bound.h == 1) {
delete this;
return;
}
//if(bound.w*bound.h == 1) {
// delete this;
// return;
//}
if(tl)
tl->free();
tl = NULL;
@ -41,6 +42,7 @@ void Quad::free() {
if(br)
br->free();
br = NULL;
delete this;
}
inline bool Quad::inbounds(int x, int y) {
@ -96,37 +98,39 @@ 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(bound.x + bound.w/2 > x) { //left
if(bound.y + bound.h/2 > y) { //top
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) {
} else { //bottom
if(bl)
return bl->lookup(x,y);
return WALL;
}
if(br)
return br->lookup(x,y);
return WALL;
} else { //right
if(bound.y + bound.h/2 > y) {
if(tr)
return tr->lookup(x,y);
} else {
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)
if(bound.w*bound.h == 1) {
std::cout << bound.x << "," << bound.y << ":" << data << "\n";
}
/*if(data == WALL)
std::cout << "WALL\n";
if(data == STONE)
std::cout << "STONE\n";
if(data == GRASS)
std::cout << "GRASS\n";
std::cout << "GRASS\n";*/
if(tl)
tl->print(spaces+2);
if(tr)
@ -156,7 +160,21 @@ Map::~Map() {
}
void Map::loadFromFile(std::string fpath) {
FILE* f = fopen(fpath.c_str(),"rb");
cv::Mat image = cv::imread(fpath.c_str());
std::cout << "Image size - Width: " << image.cols << " Height: " << image.rows << "\n";
tiles->init(image.cols,image.rows);
for(int x = 0; x < image.cols; ++x) {
for(int y = 0; y < image.rows; ++y) {
cv::Vec3b pixel = image.at<cv::Vec3b>(y,x); //Transposed...
Tile t = tileIndex(pixel.val[0],pixel.val[1],pixel.val[2]);
//std::cout << "Tile at " << x << "," << y << " is " << t << "\n";
tiles->insert(x,y,t);
}
}
/* FILE* f = fopen(fpath.c_str(),"rb");
int w,h;
fread(&w,sizeof(int),1,f);
fread(&h,sizeof(int),1,f);
@ -169,13 +187,13 @@ void Map::loadFromFile(std::string fpath) {
tiles->insert(i/w,i%w,static_cast<Tile>(t));
//std::cout << "Loading " << t << "\n";
}
fclose(f);
numTilesX = w;
numTilesY = h;
fclose(f);*/
numTilesX = image.cols;
numTilesY = image.rows;
}
void Map::render(SDL_Rect camera) {
std::cout << "Rendering map\n";
//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);
@ -183,6 +201,7 @@ void Map::render(SDL_Rect camera) {
textures[t].render(x*TILE_SIZE - camera.x,y*TILE_SIZE - camera.y);
}
}
//exit(1);
}
void Map::print() {

8
app/action/Map.h

@ -4,6 +4,7 @@
#include <iostream>
#include <SDL2/SDL.h>
#include <opencv2/opencv.hpp>
#include "Texture.h"
@ -20,7 +21,12 @@ static const int NUM_TILE_IDs = 3;
enum Tile {
WALL,
STONE,
GRASS
GRASS,
WATER,
PIT,
UNUSED1,
UNUSED2,
UNUSED3
};
class Quad {

BIN
app/action/action

BIN
app/action/assets/Cave.dab

BIN
app/action/assets/Cave.png

After

Width: 1920  |  Height: 1080  |  Size: 113 KiB

BIN
app/action/assets/test.png

After

Width: 129  |  Height: 128  |  Size: 766 B

BIN
app/action/assets/test2.png

After

Width: 16  |  Height: 16  |  Size: 212 B

BIN
app/action/assets/test3.png

After

Width: 4  |  Height: 4  |  Size: 156 B

33
app/action/main.cpp

@ -10,8 +10,8 @@
#include "Map.h"
//screen width, height
const int S_W = 640;
const int S_H = 480;
const int S_W = 1280;
const int S_H = 960;
int L_W = -1;
int L_H = -1;
@ -35,9 +35,32 @@ int frames_total;
int main(int argc, char** argv) {
init();
/*
Quad* tree = new Quad();
int size = 8;
tree->init(size,size);
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
tree->insert(x,y,static_cast<Tile>((x+y)/2));
}
}
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
printf("(%d,%d), expected %d and tile is %d\n",x,y,(x+y)/2,tree->lookup(x,y));
}
}
//tree->print(0);
tree->free();
//close();
//exit(1);
*/
Map map;
map.loadFromFile("assets/Cave.dab");
if(argc > 1) {
std::string s = argv[2];
map.loadFromFile(s);
} else {
map.loadFromFile("assets/Cave.png");
}
L_W = map.getW();
L_H = map.getH();
@ -80,7 +103,7 @@ int main(int argc, char** argv) {
SDL_RenderPresent(renderer);
float avgFPS = frames_total / (fpsTimer.getTicks() / 1000.0f);
printf("FPS: %f\n",avgFPS);
//printf("FPS: %f\n",avgFPS);
++frames_total;
int frameTicks = capTimer.getTicks();

18
app/action/test.cpp

@ -3,14 +3,24 @@
const int S_W = 640;
const int S_H = 480;
const int L_W = 1920;
const int L_H = 1080;
int L_W = 1920;
int L_H = 1080;
int main() {
Quad* tree = new Quad();
for(int x = 0; x < L_W; ++x) {
for(int y = 0; y < L_H; ++y) {
tree->insert(x,y,STONE);
}
}
tree->free();
/*
Map m;
m.loadFromFile("assets/Cave.bmp");
m.print();
*/
return 0;
}

BIN
app/action/treetest

BIN
app/procgen/Cave.png

After

Width: 1920  |  Height: 1080  |  Size: 115 KiB

2
app/procgen/cave.py

@ -77,7 +77,7 @@ def writeDab(fname,im):
def driver():
c = genCave()
writeDab("Cave.dab",c)
#writeDab("Cave.dab",c)
cv2.imwrite("Cave.png",c)
driver()
Loading…
Cancel
Save