Maploading

This commit is contained in:
rendo 2026-03-12 11:01:15 +05:00
commit a2dffd7cb9
8 changed files with 82 additions and 36 deletions

View file

@ -2,9 +2,11 @@ CP=clang
CFLAGS=$(shell cat compiler_flags.txt | tr '\n' ' ') CFLAGS=$(shell cat compiler_flags.txt | tr '\n' ' ')
FILES=$(wildcard src/*.cpp) FILES=$(wildcard src/*.cpp)
build/raylib-test-linux.x86_64 : $(FILES) .PHONY : clean cross-windows
mkdir -p build
$(CP) $(FILES) $(CFLAGS) -o build/raylib-test-linux.x86_64 build/linux/raylib-test-linux.x86_64 : $(FILES)
mkdir -p build/linux
$(CP) $(FILES) $(CFLAGS) -o build/linux/raylib-test-linux.x86_64
clean : clean :

View file

@ -1,6 +1,5 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "components.h"
#include "world.h" #include "world.h"
#include "maploader.h" #include "maploader.h"
@ -8,21 +7,21 @@ const int SCREEN_HEIGHT = 320;
const int SCREEN_WIDTH = 320; const int SCREEN_WIDTH = 320;
int main() { int main() {
loadmap();
return 0;
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test game"); InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test game");
SetTargetFPS(60); SetTargetFPS(60);
world = create_world_with(1./10.); world = create_world_with(1./10.);
world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2})); loadmap(world);
world.setup(); world.setup();
while (WindowShouldClose() == false) { while (WindowShouldClose() == false) {
if(IsKeyPressed(KEY_F1))
world.debug = !world.debug;
world.process(); world.process();
BeginDrawing(); BeginDrawing();

View file

@ -1,17 +1,47 @@
#include "maploader.h" #include "maploader.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "components.h"
#include "world.h" #include "world.h"
void loadmap() { void loadmap(World& world) {
std::ifstream input_stream; std::ifstream in;
input_stream.open("assets/map"); in.open("assets/map");
if(input_stream.is_open() == false) int x = 0;
return; int y = 0;
std::cout << input_stream.getline() while(in.eof() == false)
{
char c;
in.get(c);
input_stream.close(); switch (c) {
case '#':
{
Wall* wall = new Wall;
wall->position = {(float)x*16,(float)y*16};
world.entities.push_back(wall);
x++;
}
break;
case 'P':
{
Pacman* pacman = new Pacman;
pacman->position = {(float)x*16,(float)y*16};
world.entities.push_back(pacman);
x++;
}
break;
case '\n':
x = 0;
y++;
break;
default:
x++;
break;
}
}
} }

View file

@ -1,6 +1,8 @@
#ifndef MAPLOADER_H #ifndef MAPLOADER_H
#define MAPLOADER_H #define MAPLOADER_H
void loadmap(); #include "world.h"
void loadmap(World& world);
#endif #endif

View file

@ -71,22 +71,5 @@ void Pacman::draw() const {
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
} }
// Wall definitions
Wall::Wall() {
this->texture = LoadTexture("assets/sprites/walls.png");
}
Wall::~Wall() {
UnloadTexture(this->texture);
}
void Wall::ready() {
// Set neigbors depending on state
}
void Wall::draw() const {
// Draw based on neighbors
}

19
src/wall.cpp Normal file
View file

@ -0,0 +1,19 @@
#include "components.h"
#include <raylib.h>
Wall::Wall() {
this->texture = LoadTexture("assets/sprites/wall.png");
}
Wall::~Wall() {
UnloadTexture(this->texture);
}
void Wall::ready() {
// Set neigbors depending on state
}
void Wall::draw() const {
// Draw based on neighbors
DrawTexture(this->texture, this->position.x, this->position.y, WHITE);
}

View file

@ -5,6 +5,7 @@ World::World(){
this->entities = {}; this->entities = {};
this->seconds_per_tick = 0; this->seconds_per_tick = 0;
this->clock = 0; this->clock = 0;
this->debug = false;
} }
World::~World(){ World::~World(){
@ -21,6 +22,11 @@ void World::setup(){
void World::process(){ void World::process(){
this->clock += GetFrameTime(); this->clock += GetFrameTime();
if(IsKeyPressed(KEY_F2))
this->seconds_per_tick-=0.025;
if(IsKeyPressed(KEY_F3))
this->seconds_per_tick+=0.025;
this->update_grid(); this->update_grid();
for(int i = 0; i < this->entities.size(); i++) { for(int i = 0; i < this->entities.size(); i++) {
@ -39,6 +45,11 @@ void World::draw() const {
for(int i = 0; i < this->entities.size(); i++) { for(int i = 0; i < this->entities.size(); i++) {
this->entities[i]->draw(); this->entities[i]->draw();
} }
if (this->debug == false)
return;
DrawText(TextFormat("entities: %i",this->entities.size()), 0, 0, 14, WHITE);
DrawText(TextFormat("seconds_per_tick: %f",this->seconds_per_tick), 0, 16, 14, WHITE);
} }
int indexify_position(Vector2 vector){ int indexify_position(Vector2 vector){

View file

@ -13,7 +13,7 @@ class World {
public: public:
World(); World();
~World(); ~World();
bool debug;
std::vector<Entity*> entities; /// Main subjects of game world. std::vector<Entity*> entities; /// Main subjects of game world.
Entity* grid[GRID_CAPACITY]; /// Grid representation Entity* grid[GRID_CAPACITY]; /// Grid representation
float seconds_per_tick; /// Internal clock speed float seconds_per_tick; /// Internal clock speed