summaryrefslogtreecommitdiff
path: root/Week1-Pacman
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2024-12-23 12:32:54 +0300
committerBoredGuy <osome3717@gmail.com>2024-12-23 12:32:54 +0300
commit61252071880c625b367623191cc86b3a98b8d562 (patch)
tree4718d62c7b13e4f7482a6ee7cdb33cec3e5d69b4 /Week1-Pacman
parentac7ba6dc94f6eb2d8cf9092391714247bb947148 (diff)
Work started on adding a level system
- Levels load from tiled CSV export - Levels also draw We need to add collisions with the level and adjust the level's width and height to fit well in a 4:3 window
Diffstat (limited to 'Week1-Pacman')
-rw-r--r--Week1-Pacman/assets/Maps/maze.csv31
-rw-r--r--Week1-Pacman/assets/Sprites/BigCoin.pngbin0 -> 344 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/BigCoinTransparent.pngbin0 -> 349 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/Coin.pngbin0 -> 322 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/CoinTransparent.pngbin0 -> 309 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/PacMan.pngbin0 -> 639 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/Tileset.pngbin0 -> 1444 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/atlas.pngbin0 -> 1699 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/blueGhost.pngbin0 -> 615 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/greenGhost.pngbin0 -> 571 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/orangeGhost.pngbin0 -> 629 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/redGhost.pngbin0 -> 572 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/sprites.pngbin0 -> 4698 bytes
-rw-r--r--Week1-Pacman/assets/Sprites/yellowGhost.pngbin0 -> 571 bytes
-rw-r--r--Week1-Pacman/src/main.c32
-rw-r--r--Week1-Pacman/src/map.c115
-rw-r--r--Week1-Pacman/src/map.h20
17 files changed, 186 insertions, 12 deletions
diff --git a/Week1-Pacman/assets/Maps/maze.csv b/Week1-Pacman/assets/Maps/maze.csv
new file mode 100644
index 0000000..fa517af
--- /dev/null
+++ b/Week1-Pacman/assets/Maps/maze.csv
@@ -0,0 +1,31 @@
+283,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,286
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,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1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,358
+391,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,392,394
diff --git a/Week1-Pacman/assets/Sprites/BigCoin.png b/Week1-Pacman/assets/Sprites/BigCoin.png
new file mode 100644
index 0000000..773569b
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/BigCoin.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/BigCoinTransparent.png b/Week1-Pacman/assets/Sprites/BigCoinTransparent.png
new file mode 100644
index 0000000..5e0b453
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/BigCoinTransparent.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/Coin.png b/Week1-Pacman/assets/Sprites/Coin.png
new file mode 100644
index 0000000..8fcdaf8
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/Coin.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/CoinTransparent.png b/Week1-Pacman/assets/Sprites/CoinTransparent.png
new file mode 100644
index 0000000..09779bf
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/CoinTransparent.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/PacMan.png b/Week1-Pacman/assets/Sprites/PacMan.png
new file mode 100644
index 0000000..490ebf0
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/PacMan.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/Tileset.png b/Week1-Pacman/assets/Sprites/Tileset.png
new file mode 100644
index 0000000..b0cc61a
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/Tileset.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/atlas.png b/Week1-Pacman/assets/Sprites/atlas.png
new file mode 100644
index 0000000..6d4af65
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/atlas.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/blueGhost.png b/Week1-Pacman/assets/Sprites/blueGhost.png
new file mode 100644
index 0000000..4d15d59
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/blueGhost.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/greenGhost.png b/Week1-Pacman/assets/Sprites/greenGhost.png
new file mode 100644
index 0000000..6e6454f
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/greenGhost.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/orangeGhost.png b/Week1-Pacman/assets/Sprites/orangeGhost.png
new file mode 100644
index 0000000..8c5ff93
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/orangeGhost.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/redGhost.png b/Week1-Pacman/assets/Sprites/redGhost.png
new file mode 100644
index 0000000..de4bdde
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/redGhost.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/sprites.png b/Week1-Pacman/assets/Sprites/sprites.png
new file mode 100644
index 0000000..150ca37
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/sprites.png
Binary files differ
diff --git a/Week1-Pacman/assets/Sprites/yellowGhost.png b/Week1-Pacman/assets/Sprites/yellowGhost.png
new file mode 100644
index 0000000..1a845ce
--- /dev/null
+++ b/Week1-Pacman/assets/Sprites/yellowGhost.png
Binary files differ
diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c
index e4113be..373d795 100644
--- a/Week1-Pacman/src/main.c
+++ b/Week1-Pacman/src/main.c
@@ -3,6 +3,7 @@
#include <stdbool.h>
#include "demo.h"
#include "pacman.h"
+#include "map.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
@@ -25,10 +26,16 @@ int main() {
SDL_Event e;
struct pacman pacman = {0};
+
+ map_init(&demo);
+
+ struct map map = {0};
+ load_map("assets/Maps/maze.csv", &map);
+
for(int i = 0; i < 4; i++)
init_animation(&pacman.animations[i], &(struct animation_init){
.ren = demo.ren,
- .spritesheet = "assets/sprites.png",
+ .spritesheet = "assets/Sprites/sprites.png",
.initial_angle = 90.0 * i,
.initial_frame_count = 3,
@@ -36,21 +43,21 @@ int main() {
.initial_frames = (SDL_Rect[]) {
{
.x = 0,
- .y = 190,
- .w = 120,
- .h = 130
+ .y = 285,
+ .w = 180,
+ .h = 195
},
{
- .x = 160,
- .y = 190,
- .w = 120,
- .h = 130
+ .x = 240,
+ .y = 285,
+ .w = 180,
+ .h = 195
},
{
- .x = 310,
- .y = 190,
- .w = 130,
- .h = 130
+ .x = 465,
+ .y = 285,
+ .w = 195,
+ .h = 195
}
}
});
@@ -74,6 +81,7 @@ int main() {
SDL_RenderClear(demo.ren);
update_pacman(&pacman, dt);
update_demo(&demo);
+ draw_map(&demo, &map);
draw_pacman(&demo, &pacman);
SDL_RenderPresent(demo.ren);
}
diff --git a/Week1-Pacman/src/map.c b/Week1-Pacman/src/map.c
new file mode 100644
index 0000000..0610fd4
--- /dev/null
+++ b/Week1-Pacman/src/map.c
@@ -0,0 +1,115 @@
+#include <stdio.h>
+#include <errno.h>
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+#include "map.h"
+
+#define TILE_SIDE 8
+
+SDL_Texture* map_texture = NULL;
+
+void add_map_row(const char* string, int* row, int width);
+
+void load_map(const char* filename, struct map* map) {
+ if(map->contents != NULL) {
+ printf("Reallocating non empty map, exitting!\n");
+ exit(1);
+ }
+
+ FILE* file = fopen(filename, "r");
+
+ if(file == NULL) {
+ const char* error;
+
+ switch(errno) {
+ default:
+ error = "Unknown Error";
+ }
+
+ printf("Failed to load map %s, error: %s, exitting!\n",filename, error);
+ exit(1);
+ }
+
+ map->height = 0;
+ char buffer[1024];
+
+ fgets(buffer, 1024, file);
+ buffer[1023] = '\0';
+
+ map->width = 1;
+ for (unsigned long i = 0; i < strlen(buffer); i++) {
+ if (buffer[i] == ',')
+ map->width++;
+ }
+ fseek(file, 0, SEEK_SET);
+
+ while(fgets(buffer, 1024, file)) {
+ //Add another row to the map
+ map->height++;
+ map->contents =
+ realloc(map->contents, map->width * map->height * sizeof(int));
+
+ add_map_row(buffer,
+ &map->contents[map->width * (map->height - 1)],
+ map->width);
+ }
+
+ fclose(file);
+}
+
+void add_map_row(const char* line, int* row, int width) {
+ for(int i = 0; i < width; i++) {
+ sscanf(line, "%d", &row[i]);
+
+ int length = 1;
+ int j = row[i];
+
+ if(j < 0) {
+ length++;
+ j = -j;
+ }
+ for(; j > 0; j /= 10, length++);
+
+ line += length;
+ }
+}
+
+void draw_map(struct demo* demo, struct map* map) {
+
+ for(int i = 0; i < map->height; i++)
+ for(int j = 0; j < map->width; j++) {
+ int tile =
+ map->contents[i * map->width + j];
+
+ if(tile == -1) {
+ continue;
+ }
+
+ SDL_Rect s = {
+ .x = (tile % 36) * TILE_SIDE,
+ .y = (tile / 36) * TILE_SIDE,
+ .w = TILE_SIDE,
+ .h = TILE_SIDE
+ };
+
+ SDL_Rect d = {
+ .x = j * 26,
+ .y = i * 20,
+ .w = 26,
+ .h = 20
+ };
+
+ demo_rendercopy(demo, map_texture, &s, &d);
+ }
+}
+
+void map_init(struct demo* demo) {
+ map_texture =
+ IMG_LoadTexture(demo->ren, "assets/Sprites/Tileset.png");
+
+ if(map_texture == NULL) {
+ printf("Failed to load map tileset, error: %s, exitting!\n",
+ SDL_GetError());
+ exit(1);
+ }
+}
diff --git a/Week1-Pacman/src/map.h b/Week1-Pacman/src/map.h
new file mode 100644
index 0000000..2161806
--- /dev/null
+++ b/Week1-Pacman/src/map.h
@@ -0,0 +1,20 @@
+#ifndef MAP_H_
+#define MAP_H_
+
+#include "demo.h"
+
+struct map {
+ int* contents;
+
+ int width;
+ int height;
+};
+
+/*
+ * Load map exported as CSV file from tiled.
+*/
+void load_map(const char* filename, struct map* map);
+void draw_map(struct demo* demo, struct map* map);
+void map_init(struct demo* demo);
+
+#endif // MAP_H_