Bläddra i källkod

Started C++ conversion

Florian DORMONT 10 år sedan
förälder
incheckning
7b2fb0c448
10 ändrade filer med 224 tillägg och 93 borttagningar
  1. 12 6
      Makefile
  2. 31 0
      include/Camera.h
  3. 26 0
      include/Entity.h
  4. 28 0
      include/Map.h
  5. 0 17
      include/map.h
  6. 22 0
      src/Camera.cpp
  7. 21 0
      src/Entity.cpp
  8. 43 0
      src/Map.cpp
  9. 41 9
      src/main.c
  10. 0 61
      src/map.c

+ 12 - 6
Makefile

@@ -2,8 +2,8 @@ NAME = WalrusRPG
 
 DEBUG = FALSE
 
-CC = nspire-gcc
-CFLAGS = -Wall -W -marm -std=gnu11 -I include -I art
+CC = nspire-g++
+CFLAGS = -Wall -W -marm -std=gnu++98 -I include -I art
 
 ifeq ($(DEBUG),FALSE)
 	CFLAGS += -Ofast -flto
@@ -14,18 +14,23 @@ endif
 ZEHN = genzehn
 ZEHNFLAGS = --name "$(NAME)"
 
-SOURCES = art/sprites.c $(wildcard src/*.c)
-OBJS = $(patsubst %.c,%.o,$(SOURCES))
+INCDIR = include
+SRCDIR = src
+
+SOURCES_C = art/sprites.c $(wildcard SRCDIR/*.c)
+SOURCES_CPP = $(wildcard SRCDIR/*.cpp)
+OBJS = $(patsubst %.c,%.o,$(SOURCES_C)) $(patsubst %.cpp,%.o,$(SOURCES_CPP)) 
 
 DISTDIR = bin
 ELF = $(DISTDIR)/$(NAME).elf
 EXE = $(DISTDIR)/$(NAME).tns
 
+
 all: $(EXE)
 
-%.o: %.c
+%.o: %.c %.cpp
 	@echo "CC: $@"
-	@$(CC) $(CFLAGS) -c $< -o $@
+	@$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
 
 art/sprites.c:
 	@$(MAKE) -C art/
@@ -41,6 +46,7 @@ $(EXE): $(ELF)
 	@$(ZEHN) --input $(ELF) --output $(EXE) $(ZEHNFLAGS)
 
 clean:
+	echo $(OBJS)
 	rm -rf $(DISTDIR)
 	rm -f $(OBJS)
 	@$(MAKE) -C art/ clean

+ 31 - 0
include/Camera.h

@@ -0,0 +1,31 @@
+#ifndef INCLUDE_CAMERA_H
+#define INCLUDE_CAMERA_H
+
+namespace WalrusRPG {
+	class Camera
+	{
+	protected:
+		
+		// Do you want to use classes for coordinates and allow them to have vector-based mathematics? With operator overriding that could be doable to have
+		// Vector2 a(3, 2); Vector2 b(1, 2); Vector3 c = a+b;
+		// Vector2 destination;
+		// Vector2 velocity;
+		// Vector2 acceleration;
+
+	public:
+		// TODO!: TEMPOARY PUBLIC VARIABLES. Need to make getter/setters.
+		// So, how will track camera's position? Top left or center?
+		unsigned x; // You'll probably want to switch over signed coordonates.
+		unsigned y;
+		unsigned render_area_width; // What if you only want to display the map on a part of the screen?
+		unsigned render_area_height;
+
+		Camera();
+		~Camera();
+		// This doesn't need any render as it's the utility which helps rendering. Unless you want to show debnug things.
+		// void render(float dt) const;
+		void update(float dt);
+	};
+}
+
+#endif

+ 26 - 0
include/Entity.h

@@ -0,0 +1,26 @@
+#ifndef INCLUDE_ENTITY_H
+#define INCLUDE_ENTITY_H
+
+#include "Camera.h"
+
+namespace WalrusRPG {
+	/**
+	 * Well, for now, this class will be a non abstract for ALPHA PROGRAMMING REASONS.
+	 * Expect this sooner or later to be abstract.
+	 * I don't know at this moment how will we manage the different classes heriting or compositing from this, if we use components or heritance.
+	 */
+	class Entity {
+	protected:
+		unsigned x;
+		unsigned y;
+		unsigned width;
+		unsigned height;
+	public:
+		Entity();
+		~Entity();
+		void render(Camera &camera, float dt) const;
+		void update(float dt);
+
+	};
+}
+#endif

+ 28 - 0
include/Map.h

@@ -0,0 +1,28 @@
+#ifndef INCLUDE_MAP_H
+#define INCLUDE_MAP_H
+
+#include "Camera.h"
+#include "Entity.h"
+
+namespace WalrusRPG {
+	class Map
+	{
+	protected:
+		// <Tiles> data;
+		// <Tileset> tileset;
+		unsigned int width;
+		unsigned int height;
+		unsigned *layer0;
+		unsigned *layer1;
+
+	public:
+
+		Map(int width, int height, unsigned *layer0, unsigned *layer1);
+		~Map();
+		void render(Camera &camera, float dt) const;
+		void update(float dt);
+		bool entity_collide(Entity &entity) const;
+	};
+}
+
+#endif

+ 0 - 17
include/map.h

@@ -1,17 +0,0 @@
-#ifndef INCLUDE_MAP_H
-#define INCLUDE_MAP_H
-
-typedef struct Map Map_t;
-struct Map
-{
-	unsigned w;
-	unsigned h;
-	unsigned *layer0;
-	unsigned *layer1;
-};
-
-void map_draw(unsigned x, unsigned y, const Map_t *map);
-unsigned map_collide(unsigned x, unsigned y, const Map_t *map);
-void map_loop(unsigned x, unsigned y, Map_t *map);
-
-#endif

+ 22 - 0
src/Camera.cpp

@@ -0,0 +1,22 @@
+#include "Camera.h"
+#include "Entity.h"
+
+#define CAMERA WalrusRPG::Camera
+
+CAMERA::Camera() {
+	// TODO
+}
+
+CAMERA::~Camera() {
+	// TODO if you allocate dynamically members
+}
+
+void CAMERA::update(float dt) {
+	// TODO update map's data according to elasped time
+	/*
+	
+		// Need to think aagain on how to go to a target point and/or we need to align the corner OR the center to this point.
+		position += velocity * dt;
+		velocity += acceleration * dt;
+	 */
+}

+ 21 - 0
src/Entity.cpp

@@ -0,0 +1,21 @@
+#include "Entity.h"
+
+#define Entity WalrusRPG::Entity
+
+Entity::Entity() {
+	// TODO
+}
+
+Entity::~Entity() {
+	// TODO if you allocate dynamically members
+}
+
+void Entity::update(float dt) {
+	// TODO update map's data according to elasped time
+	/*
+	
+		// Need to think aagain on how to go to a target point and/or we need to align the corner OR the center to this point.
+		position += velocity * dt;
+		velocity += acceleration * dt;
+	 */
+}

+ 43 - 0
src/Map.cpp

@@ -0,0 +1,43 @@
+#include "Map.h"
+#include "Camera.h"
+#include "graphics.h"
+#include "sprites.h"
+
+#define MAP WalrusRPG::Map
+
+MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1) {
+	this->width = width;
+	this->height = height;
+	this->layer0 = layer0;
+	this->layer1 = layer1;
+}
+
+MAP::~Map() {
+	// TODO if you allocate dynamically members
+}
+
+void MAP::update(float dt) {
+	// TODO update map's data according to elasped time
+}
+
+void MAP::render(WalrusRPG::Camera &camera, float dt) {
+	unsigned offset_x = camera.x % 24 * -1;
+	unsigned offset_y = camera.y % 24 * -1;
+
+	Rect_t sprite;
+	sprite.y = 0;
+	sprite.w = 24;
+	sprite.h = 24;
+
+	for (unsigned j = 0; j < 11; j++) {
+		for (unsigned i = 0; i < 15; i++)
+		{
+			sprite.x = map->layer0[(camera.x / 24 + i) + (camera.y / 24 + j) * map->w] * 24;
+			draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
+		}		
+	}
+}
+
+bool MAP::entity_collide(Entity &entity) {
+	return false;
+}

+ 41 - 9
src/main.c

@@ -1,7 +1,43 @@
 #include <os.h>
-#include <timers.h>
-#include <graphics.h>
-#include <map.h>
+#include "timers.h"
+#include "graphics.h"
+#include "Map.h"
+#include "Camera.h"
+
+using namespace WalrusRPG;
+
+void map_loop(unsigned x, unsigned y, Map &map)
+{
+	timer_mode(0, 0b0000010); // Free-running, no interrupts, divider = 1, 32 bit, wrapping
+	timer_load(0, 0);
+	unsigned loop_time = 546; // 32768Hz/60ups
+	unsigned loop_next = -loop_time;
+
+	unsigned keep_running = 1;
+	Camera camera;
+
+	while (keep_running)
+	{
+		if (isKeyPressed(KEY_NSPIRE_ESC)) keep_running = 0;
+		if (isKeyPressed(KEY_NSPIRE_5)) camera.y++;
+		if (isKeyPressed(KEY_NSPIRE_8)) camera.y--;
+		if (isKeyPressed(KEY_NSPIRE_6)) camera.x++;
+		if (isKeyPressed(KEY_NSPIRE_4)) camera.x--;
+
+		// Frameskip
+		if (timer_read(0) > loop_next)
+		{
+			map.render(camera, 1);
+			buffer_swap();
+		}
+
+		// Frame limiting
+		while (timer_read(0) > loop_next);
+		loop_next -= loop_time;
+	}
+}
+
+
 
 int main(int argc, char *argv[])
 {
@@ -11,9 +47,6 @@ int main(int argc, char *argv[])
 	buffer_allocate();
 	timer_init(0);
 
-	Map_t map;
-	map.w = 15;
-	map.h = 21;
 
 	unsigned mapdata0[] =
 	{
@@ -65,11 +98,10 @@ int main(int argc, char *argv[])
 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 	};
 
+	Map map(15, 21, mapdata0, mapdata1);
 
-	map.layer0 = mapdata0;
-	map.layer1 = mapdata1;
 
-	map_loop(0, 0, &map);
+	map_loop(0, 0, map);
 
 	timer_restore(0);
 	buffer_free();

+ 0 - 61
src/map.c

@@ -1,61 +0,0 @@
-#include <os.h>
-#include <timers.h>
-#include <graphics.h>
-#include <sprites.h>
-#include <map.h>
-
-void map_draw(unsigned x, unsigned y, const Map_t *map)
-{
-	unsigned offset_x = x % 24 * -1;
-	unsigned offset_y = y % 24 * -1;
-
-	Rect_t sprite;
-	sprite.y = 0;
-	sprite.w = 24;
-	sprite.h = 24;
-
-	for (unsigned j = 0; j < 11; j++)
-	for (unsigned i = 0; i < 15; i++)
-	{
-		sprite.x = map->layer0[(x / 24 + i) + (y / 24 + j) * map->w] * 24;
-		draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
-	}
-}
-
-unsigned map_collide(unsigned x, unsigned y, const Map_t *map)
-{
-	(void) x;
-	(void) y;
-	(void) map;
-	return 0;
-}
-
-void map_loop(unsigned x, unsigned y, Map_t *map)
-{
-	timer_mode(0, 0b0000010); // Free-running, no interrupts, divider = 1, 32 bit, wrapping
-	timer_load(0, 0);
-	unsigned loop_time = 546; // 32768Hz/60ups
-	unsigned loop_next = -loop_time;
-
-	unsigned keep_running = 1;
-	while (keep_running)
-	{
-		if (isKeyPressed(KEY_NSPIRE_ESC)) keep_running = 0;
-		if (isKeyPressed(KEY_NSPIRE_5)) y++;
-		if (isKeyPressed(KEY_NSPIRE_8)) y--;
-		if (isKeyPressed(KEY_NSPIRE_6)) x++;
-		if (isKeyPressed(KEY_NSPIRE_4)) x--;
-
-		// Frameskip
-		if (timer_read(0) > loop_next)
-		{
-			map_draw(x, y, map);
-			buffer_swap();
-		}
-
-		// Frame limiting
-		while (timer_read(0) > loop_next);
-		loop_next -= loop_time;
-	}
-}
-