summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2021-02-28 23:29:25 +0300
committerAleksey Veresov <aleksey@veresov.pro>2021-02-28 23:29:25 +0300
commita365de1b0ab91f9c057196a601d96d2aed6e48fb (patch)
tree4c3262c1745337dbd4e6c87525edc0532b517963
parent4cfd9c6b96035ca7ccf6444e911b2a908d8500eb (diff)
downloadtakethis-a365de1b0ab91f9c057196a601d96d2aed6e48fb.tar
takethis-a365de1b0ab91f9c057196a601d96d2aed6e48fb.tar.xz
takethis-a365de1b0ab91f9c057196a601d96d2aed6e48fb.zip
o_O
-rw-r--r--Makefile2
-rw-r--r--data/anim.bmpbin22666 -> 0 bytes
-rw-r--r--data/boss.bmpbin22666 -> 0 bytes
-rw-r--r--data/font.otfbin0 -> 32480 bytes
-rw-r--r--data/map5
-rw-r--r--data/map/023
-rw-r--r--data/map/127
-rw-r--r--data/map/223
-rw-r--r--data/map/323
-rw-r--r--data/map/4 (renamed from data/rooms/4)0
-rw-r--r--data/map/5 (renamed from data/rooms/5)0
-rw-r--r--data/map/6 (renamed from data/rooms/6)0
-rw-r--r--data/map/7 (renamed from data/rooms/7)0
-rw-r--r--data/map/8 (renamed from data/rooms/8)0
-rw-r--r--data/map/@21
-rw-r--r--data/map/Q (renamed from data/rooms/9)0
-rw-r--r--data/ponpon.oggbin0 -> 3143290 bytes
-rw-r--r--data/rooms/015
-rw-r--r--data/rooms/116
-rw-r--r--data/rooms/216
-rw-r--r--data/rooms/316
-rw-r--r--data/tiles.bmpbin16522 -> 0 bytes
-rw-r--r--data/txr.bmpbin0 -> 196746 bytes
-rw-r--r--src/body.c69
-rw-r--r--src/body.h26
-rw-r--r--src/game.c111
-rw-r--r--src/game.h12
-rw-r--r--src/globals.c14
-rw-r--r--src/globals.h25
-rw-r--r--src/main.c52
-rw-r--r--src/map.c246
-rw-r--r--src/map.h18
-rw-r--r--src/player.c61
-rw-r--r--src/player.h29
-rw-r--r--src/room.c59
-rw-r--r--src/room.h27
-rw-r--r--src/texture.c22
-rw-r--r--src/texture.h23
38 files changed, 781 insertions, 200 deletions
diff --git a/Makefile b/Makefile
index ede3445..977ea63 100644
--- a/Makefile
+++ b/Makefile
@@ -17,7 +17,7 @@ else
CFLAGS += -O3
endif
# Use SDL:
-LFLAGS = -lSDL2
+LFLAGS = -lSDL2 -lSDL2_ttf -lSDL2_mixer
# Directories definitions:
BUILD = build
diff --git a/data/anim.bmp b/data/anim.bmp
deleted file mode 100644
index a59ced7..0000000
--- a/data/anim.bmp
+++ /dev/null
Binary files differ
diff --git a/data/boss.bmp b/data/boss.bmp
deleted file mode 100644
index a59ced7..0000000
--- a/data/boss.bmp
+++ /dev/null
Binary files differ
diff --git a/data/font.otf b/data/font.otf
new file mode 100644
index 0000000..f460ca0
--- /dev/null
+++ b/data/font.otf
Binary files differ
diff --git a/data/map b/data/map
deleted file mode 100644
index 048494e..0000000
--- a/data/map
+++ /dev/null
@@ -1,5 +0,0 @@
-0 = 1 = 2 . 6 = 7
-. . | . . . | . .
-. . 3 = 4 = 5 = 8
-. . . . . . . . |
-. . . . . . . . 9
diff --git a/data/map/0 b/data/map/0
new file mode 100644
index 0000000..719c8ff
--- /dev/null
+++ b/data/map/0
@@ -0,0 +1,23 @@
+0
+.1..
+#0#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#1
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . $1. . ^3. . . . . . #5
+#7. . ^1. . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . $ . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . $1. . . . . . . . . . . . . . X2
+#7. . . . . . . . . . . . . . . . . . X3
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . $ . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . ^2. . . ^1. . . . #5
+#7. . . . ^ . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#2#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#3
++000+000
++000+000
++000+000
++000+000
++000+000
diff --git a/data/map/1 b/data/map/1
new file mode 100644
index 0000000..93ee8bd
--- /dev/null
+++ b/data/map/1
@@ -0,0 +1,27 @@
+0
+.230
+#0#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#1
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . $ . . . . . ^ . . . . . . . $ . #5
+#7. . . . . . . . . . . . . . ^ . . . #5
+#7. . . . . $ . . . . . . $ . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . $ . . ^3^ ^1^ ^2^ . . . . . . #5
+X7. . . . . . . . . . . ^3. . . . . . X2
+X6. . . . . . . . . . . ^1. . . ^2. . X3
+#7. . . . ^ . $ . . . . ^ . . . . . . #5
+#7. . . . ^2. . . . . . . . $ . . . . #5
+#7. . $ . ^3. . . . . . . . . . . . . #5
+#7. . . . . . . . . $ . . . ^1. . . . #5
+#7. . . . . . . . . . . . . . . ^ . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#2#6#6#6#6#6#6#6#6X4X5#6#6#6#6#6#6#6#6#3
++030+050
+-050+100
++065-054
++052-045
+-098+034
++000+000
++040-023
+-047-073
+-034-078
diff --git a/data/map/2 b/data/map/2
new file mode 100644
index 0000000..2b0c09d
--- /dev/null
+++ b/data/map/2
@@ -0,0 +1,23 @@
+0
+...1
+#0#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#1
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . $1. . ^3. . . . . . #5
+#7. . ^1. . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . $ . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+X7. . . $1. . . . . . . . . . . . . . #5
+X6. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . $ . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . ^2. . . ^1. . . . #5
+#7. . . . ^ . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#2#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#3
++000+000
++000+000
++000+000
++000+000
++000+000
diff --git a/data/map/3 b/data/map/3
new file mode 100644
index 0000000..9b917ef
--- /dev/null
+++ b/data/map/3
@@ -0,0 +1,23 @@
+0
+1...
+#0#4#4#4#4#4#4#4#4X0X1#4#4#4#4#4#4#4#4#1
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . $1. . ^3. . . . . . #5
+#7. . ^1. . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . $ . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . $1. . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#7. . $ . . . . . . . . . . . . . . . #5
+#7. . . . . . . . . ^2. . . ^1. . . . #5
+#7. . . . ^ . . . . . . . . . . . . . #5
+#7. . . . . . . . . . . . . . . . . . #5
+#2#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#3
++000+000
++000+000
++000+000
++000+000
++000+000
diff --git a/data/rooms/4 b/data/map/4
index a3f2532..a3f2532 100644
--- a/data/rooms/4
+++ b/data/map/4
diff --git a/data/rooms/5 b/data/map/5
index d72c22e..d72c22e 100644
--- a/data/rooms/5
+++ b/data/map/5
diff --git a/data/rooms/6 b/data/map/6
index 0407287..0407287 100644
--- a/data/rooms/6
+++ b/data/map/6
diff --git a/data/rooms/7 b/data/map/7
index d6185ff..d6185ff 100644
--- a/data/rooms/7
+++ b/data/map/7
diff --git a/data/rooms/8 b/data/map/8
index e689452..e689452 100644
--- a/data/rooms/8
+++ b/data/map/8
diff --git a/data/map/@ b/data/map/@
new file mode 100644
index 0000000..4ae3c97
--- /dev/null
+++ b/data/map/@
@@ -0,0 +1,21 @@
+5
+....
+#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>
+#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>
+#>#>#0#4#4#4#4#4#4#4#4#4#4#4#4#4#1#>#>#>
+#>#>#7.6. . . . . . . . . . . .6#5#>#>#>
+#>#>#7.6= . . . . . . . . . . .6#5#>#>#>
+#>#>#7.6. . . = . . . . . . . .6#5#>#>#>
+#>#>#7.6. . * . . &6. . * . . .6#5#>#>#>
+#>#>#7.6. . . . . . . . . . . .6#5#>#>#>
+#>#>#7.6. . . . . M . . . . . .6#5#>#>#>
+#>#>#7.6. . . . . . . . . . . .6#5#>#>#>
+#>#>#7.6. . . . . . . . . . . .6#5#>#>#>
+#>#>#7.6. . . . . @ . . . . . .6#5#>#>#>
+#>#>#7.6. . . . . . . . . . . .6#5#>#>#>
+#>#>#2#6#6#6#6#6#6#6#6#6#6#6#6#6#3#>#>#>
+#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>
+#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>#>
++000+000=IT'S DANGEROUS TO GO ALONE!
++000+000=TAKE THIS.
++000+000
diff --git a/data/rooms/9 b/data/map/Q
index 1d0cfe4..1d0cfe4 100644
--- a/data/rooms/9
+++ b/data/map/Q
diff --git a/data/ponpon.ogg b/data/ponpon.ogg
new file mode 100644
index 0000000..d97e166
--- /dev/null
+++ b/data/ponpon.ogg
Binary files differ
diff --git a/data/rooms/0 b/data/rooms/0
deleted file mode 100644
index a4bdbb3..0000000
--- a/data/rooms/0
+++ /dev/null
@@ -1,15 +0,0 @@
-#0#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#4#1
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#7. . . . . . . . . . . . . . . . . #5
-#3#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#6#2
diff --git a/data/rooms/1 b/data/rooms/1
deleted file mode 100644
index 18739f6..0000000
--- a/data/rooms/1
+++ /dev/null
@@ -1,16 +0,0 @@
-zelda
-# # # # # # # # # # # # # # # # # # #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-X . . . . . . . . . . . . . . . . . X
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# # # # # # # # # X # # # # # # # # #
diff --git a/data/rooms/2 b/data/rooms/2
deleted file mode 100644
index d6185ff..0000000
--- a/data/rooms/2
+++ /dev/null
@@ -1,16 +0,0 @@
-zelda
-# # # # # # # # # # # # # # # # # # #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-X . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# # # # # # # # # # # # # # # # # # #
diff --git a/data/rooms/3 b/data/rooms/3
deleted file mode 100644
index e0d902a..0000000
--- a/data/rooms/3
+++ /dev/null
@@ -1,16 +0,0 @@
-zelda
-# # # # # # # # # X # # # # # # # # #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . X
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# . . . . . . . . . . . . . . . . . #
-# # # # # # # # # # # # # # # # # # #
diff --git a/data/tiles.bmp b/data/tiles.bmp
deleted file mode 100644
index 1b40620..0000000
--- a/data/tiles.bmp
+++ /dev/null
Binary files differ
diff --git a/data/txr.bmp b/data/txr.bmp
new file mode 100644
index 0000000..8864f88
--- /dev/null
+++ b/data/txr.bmp
Binary files differ
diff --git a/src/body.c b/src/body.c
new file mode 100644
index 0000000..d0e7938
--- /dev/null
+++ b/src/body.c
@@ -0,0 +1,69 @@
+#include "body.h"
+
+#include <SDL2/SDL_ttf.h>
+#include "room.h"
+#include "globals.h"
+
+
+static void move(tt_body *b, int d)
+{
+ b->xrem += b->xvel * d;
+ b->yrem += b->yvel * d;
+ b->x += b->xrem / 1000;
+ b->y += b->yrem / 1000;
+ b->xrem = b->xrem % 1000;
+ b->yrem = b->yrem % 1000;
+}
+
+
+void tt_body_move(int d)
+{
+ tt_room *r = ttplayer.room;
+ int i, j;
+ for (i = 0; i != TT_ROOM_H; ++i) {
+ for (j = 0; j != TT_ROOM_W; ++j) {
+ tt_body *w = r->walls[i][j];
+ if (w) w->rem += d;
+ }
+ }
+ for (i = 0; i != r->bodies_count; ++i) {
+ tt_body *b = r->bodies + i;
+ b->rem += d;
+ int ex = b->x;
+ int exr = b->xrem;
+ int ey = b->y;
+ int eyr = b->yrem;
+ move(b, d);
+ {
+ SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 };
+ if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue;
+ }
+ b->x = ex;
+ b->y = ey;
+ b->xrem = exr;
+ b->yrem = eyr;
+ b->xvel *= -1;
+ move(b, d);
+ {
+ SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 };
+ if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue;
+ }
+ b->x = ex;
+ b->y = ey;
+ b->xrem = exr;
+ b->yrem = eyr;
+ b->xvel *= -1;
+ b->yvel *= -1;
+ move(b, d);
+ {
+ SDL_Rect box = { b->x + 5, b->y + 2, 22, 28 };
+ if (!tt_room_collide(r, &box) && !tt_room_out(r, &box)) continue;
+ }
+ b->x = ex;
+ b->y = ey;
+ b->xrem = exr;
+ b->yrem = eyr;
+ b->xvel *= -1;
+ move(b, d);
+ }
+}
diff --git a/src/body.h b/src/body.h
new file mode 100644
index 0000000..fcb15bc
--- /dev/null
+++ b/src/body.h
@@ -0,0 +1,26 @@
+#ifndef TT_INCLUDED_BODY
+#define TT_INCLUDED_BODY
+
+
+typedef struct tt_body {
+ int x;
+ int y;
+ int xrem;
+ int yrem;
+ int xvel;
+ int yvel;
+ int txrrow;
+ int txrcol;
+ int rem;
+ int anim;
+ int rate;
+ void (*collision_act)(struct tt_body *b);
+ char *msg;
+ int msglen;
+} tt_body;
+
+
+void tt_body_move(int d);
+
+
+#endif
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..444f9ff
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,111 @@
+#include "game.h"
+
+#include <SDL2/SDL.h>
+#include "globals.h"
+
+
+int magic = 0;
+
+static int ticks;
+
+static void step(int d)
+{
+ int xw = ttplayer.xwalk * 137;
+ int yw = ttplayer.ywalk * 137;
+ if (xw || yw) {
+ ttplayer.rem += d;
+ }
+ if (xw && yw) {
+ ttplayer.xrem += (double)(xw * d) / sqrt(2);
+ ttplayer.yrem += (double)(yw * d) / sqrt(2);
+ } else if (xw || yw) {
+ ttplayer.xrem += xw * d;
+ ttplayer.yrem += yw * d;
+ }
+ int x = ttplayer.x;
+ int y = ttplayer.y;
+ ttplayer.x += ttplayer.xrem / 1000;
+ ttplayer.y += ttplayer.yrem / 1000;
+ ttplayer.xrem = ttplayer.xrem % 1000;
+ ttplayer.yrem = ttplayer.yrem % 1000;
+ {
+ SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
+ int out = tt_room_out(ttplayer.room, &box);
+ if (out) ttplayer.room = ttplayer.room->neighbours[out - 1];
+ if (out == 1) ttplayer.y = TT_ROOM_H * 32 - 32;
+ else if (out == 2) ttplayer.x = 0;
+ else if (out == 3) ttplayer.y = 0;
+ else if (out == 4) ttplayer.x = TT_ROOM_W * 32 - 32;
+ }
+ {
+ SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
+ if (tt_room_collide(ttplayer.room, &box)) {
+ ttplayer.x = x;
+ ttplayer.y = y;
+ }
+ }
+ tt_body_move(d);
+ {
+ int i;
+ SDL_Rect box = { ttplayer.x, ttplayer.y, 32, 32 };
+ for (i = 0; i != ttplayer.room->bodies_count; ++i) {
+ tt_body *b = ttplayer.room->bodies + i;
+ SDL_Rect body = { 4 + b->x, 4 + b->y, 24, 24 };
+ if (SDL_HasIntersection(&body, &box)) b->collision_act(b);
+ }
+ }
+}
+
+
+void gotofirstroom()
+{
+ ttplayer.room = ttmap + '0';
+ magic = 0;
+ ticks = SDL_GetTicks();
+}
+
+void tt_mainloop()
+{
+ int keyw, keys, keya, keyd, arru, arrr, arrd, arrl;
+ keyw = keya = keys = keyd = arru = arrr = arrd = arrl = 0;
+ ticks = SDL_GetTicks();
+ int q = 0;
+ while (!q) {
+ SDL_Event e;
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) q = 1;
+ else if (e.type == SDL_KEYDOWN) {
+ int code = e.key.keysym.scancode;
+ if (code == SDL_SCANCODE_W) keyw = 1;
+ else if (code == SDL_SCANCODE_S) keys = 1;
+ else if (code == SDL_SCANCODE_A) keya = 1;
+ else if (code == SDL_SCANCODE_D) keyd = 1;
+ else if (code == SDL_SCANCODE_UP) arru = 1;
+ else if (code == SDL_SCANCODE_RIGHT) arrr = 1;
+ else if (code == SDL_SCANCODE_DOWN) arrd = 1;
+ else if (code == SDL_SCANCODE_LEFT) arrl = 1;
+ } else if (e.type == SDL_KEYUP) {
+ int code = e.key.keysym.scancode;
+ if (code == SDL_SCANCODE_W) keyw = 0;
+ else if (code == SDL_SCANCODE_S) keys = 0;
+ else if (code == SDL_SCANCODE_A) keya = 0;
+ else if (code == SDL_SCANCODE_D) keyd = 0;
+ else if (code == SDL_SCANCODE_UP) arru = 0;
+ else if (code == SDL_SCANCODE_RIGHT) arrr = 0;
+ else if (code == SDL_SCANCODE_DOWN) arrd = 0;
+ else if (code == SDL_SCANCODE_LEFT) arrl = 0;
+ }
+ }
+ ttplayer.ywalk = (keys | arrd) - (keyw | arru);
+ ttplayer.xwalk = (keyd | arrr) - (keya | arrl);
+ int newticks = SDL_GetTicks();
+ step(newticks - ticks);
+ ticks = newticks;
+ SDL_RenderClear(ttrdr);
+ tt_player_draw();
+ SDL_RenderPresent(ttrdr);
+ if (magic == tt_gotofirstroom) {
+ gotofirstroom();
+ }
+ }
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 0000000..6f84f24
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,12 @@
+#ifndef TT_INCLUDED_GAME
+#define TT_INCLUDED_GAME
+
+
+enum { tt_gotofirstroom = 1 };
+
+extern int magic;
+
+void tt_mainloop();
+
+
+#endif
diff --git a/src/globals.c b/src/globals.c
new file mode 100644
index 0000000..0a95a00
--- /dev/null
+++ b/src/globals.c
@@ -0,0 +1,14 @@
+#include "globals.h"
+
+
+TTF_Font *ttfont;
+
+SDL_Texture *tttxr;
+
+SDL_Renderer *ttrdr;
+
+Mix_Music *ponpon;
+
+tt_room ttmap[256];
+
+tt_player ttplayer;
diff --git a/src/globals.h b/src/globals.h
new file mode 100644
index 0000000..e4d03a1
--- /dev/null
+++ b/src/globals.h
@@ -0,0 +1,25 @@
+#ifndef TT_INCLUDED_GLOBALS
+#define TT_INCLUDED_GLOBALS
+
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_ttf.h>
+#include <SDL2/SDL_mixer.h>
+#include "room.h"
+#include "player.h"
+
+
+extern TTF_Font *ttfont;
+
+extern SDL_Texture *tttxr;
+
+extern SDL_Renderer *ttrdr;
+
+extern Mix_Music *ponpon;
+
+extern tt_room ttmap[256];
+
+extern tt_player ttplayer;
+
+
+#endif
diff --git a/src/main.c b/src/main.c
index 44f4a43..01756ae 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,32 +1,52 @@
#include <SDL2/SDL.h>
-#include "room.h"
-#include "texture.h"
+#include <SDL2/SDL_ttf.h>
+#include <SDL2/SDL_mixer.h>
+#include <stdlib.h>
+#include <time.h>
+#include "globals.h"
+#include "map.h"
+#include "player.h"
+#include "game.h"
+static SDL_Texture *loadtxr(const char *path)
+{
+ SDL_Surface *surf = SDL_LoadBMP(path);
+ SDL_Texture *res = SDL_CreateTextureFromSurface(ttrdr, surf);
+ SDL_FreeSurface(surf);
+ return res;
+}
+
int main(int argc, char **argv)
{
+ SDL_Init(SDL_INIT_EVERYTHING);
+ TTF_Init();
+ Mix_Init(MIX_INIT_OGG);
+ Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096);
SDL_Window *wdw = SDL_CreateWindow("T A K E T H I S",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
950, 540, 0);
- SDL_Renderer *rdr = SDL_CreateRenderer(wdw, -1,
- SDL_RENDERER_ACCELERATED);
- SDL_Texture *txr = tt_texture_load(rdr);
- tt_room *room = tt_room_load('0');
+ ttrdr = SDL_CreateRenderer(wdw, -1, SDL_RENDERER_ACCELERATED);
+ tttxr = loadtxr("data/txr.bmp");
+ ttfont = TTF_OpenFont("data/font.otf", 24);
+ ponpon = Mix_LoadMUS("data/ponpon.ogg");
+ tt_map_load();
- SDL_SetRenderDrawColor(rdr, 215, 174, 0, 255);
+ srand(time(0));
+ SDL_SetRenderDrawColor(ttrdr, 0, 0, 0, 255);
- int n;
- for (n = 0; n != 200; ++n) {
- SDL_RenderClear(rdr);
- tt_room_draw(rdr, txr, room);
- SDL_RenderPresent(rdr);
- SDL_Delay(25);
- }
+ tt_mainloop();
- SDL_DestroyTexture(txr);
- SDL_DestroyRenderer(rdr);
+ tt_map_free();
+ TTF_CloseFont(ttfont);
+ SDL_DestroyTexture(tttxr);
+ SDL_DestroyRenderer(ttrdr);
SDL_DestroyWindow(wdw);
+ Mix_HaltMusic();
+ Mix_FreeMusic(ponpon);
+ Mix_CloseAudio();
+ TTF_Quit();
SDL_Quit();
return 0;
}
diff --git a/src/map.c b/src/map.c
new file mode 100644
index 0000000..13894d6
--- /dev/null
+++ b/src/map.c
@@ -0,0 +1,246 @@
+#include "map.h"
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_mixer.h>
+#include <stdio.h>
+#include "room.h"
+#include "globals.h"
+#include "game.h"
+
+
+static int readnum(FILE *f)
+{
+ int neg = fgetc(f) == '-';
+ int num = fgetc(f) - '0';
+ num *= 10;
+ num += fgetc(f) - '0';
+ num *= 10;
+ num += fgetc(f) - '0';
+ return (neg ? -1 : 1) * num;
+}
+
+static SDL_Rect *newtile(int row, int col)
+{
+ SDL_Rect *r = malloc(sizeof(*r));
+ r->x = 16 * col;
+ r->y = 16 * row;
+ r->h = r->w = 16;
+ return r;
+}
+
+static void do_nothing(tt_body *b)
+{}
+
+static void gribtake(tt_body *b)
+{
+ Mix_PlayMusic(ponpon, -1);
+ b->collision_act = do_nothing;
+ b->anim = 1;
+ b->txrrow = 0;
+ b->txrcol = 15;
+ magic = tt_gotofirstroom;
+}
+
+static void loadroom(tt_room *r, FILE *f)
+{
+ int default_floor_id = fgetc(f) - '0';
+ fgetc(f);
+ int i, j;
+ for (i = 0; i != 4; ++i) r->neighbours[i] = ttmap + fgetc(f);
+ r->bodies = 0;
+ r->bodies_count = 0;
+ fgetc(f);
+ for (i = 0; i != TT_ROOM_H; ++i) {
+ for (j = 0; j != TT_ROOM_W; ++j) {
+ int type = fgetc(f);
+ int id = fgetc(f);
+ int c = id;
+ if (id == ' ') id = '0';
+ id -= '0';
+ r->floor[i][j] = 0;
+ r->walls[i][j] = 0;
+ r->roof[i][j] = 0;
+ if (type == '@') {
+ ttplayer.room = r;
+ ttplayer.x = j * 32;
+ ttplayer.y = i * 32;
+ ttplayer.xrem = 0;
+ ttplayer.yrem = 0;
+ ttplayer.xwalk = 0;
+ ttplayer.ywalk = 0;
+ ttplayer.rem = 0;
+ ttplayer.money = 0;
+ ttplayer.keys = 0;
+ ttplayer.the_key = 0;
+ ttplayer.variant = 1;
+ r->floor[i][j] = newtile(0, default_floor_id);
+ } else if (type == '#') {
+ r->walls[i][j] = malloc(sizeof(tt_body));
+ tt_body *b = r->walls[i][j];
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->xvel = 0;
+ b->yvel = 0;
+ b->rem = 0;
+ b->txrrow = 1;
+ b->txrcol = id;
+ b->anim = 1;
+ b->rate = 1;
+ b->collision_act = do_nothing;
+ b->msg = 0;
+ b->msglen = 0;
+ } else if (type == 'M') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->bodies_count++;
+ r->bodies = realloc(r->bodies,
+ sizeof(tt_body) * r->bodies_count);
+ tt_body *b = r->bodies + r->bodies_count - 1;
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->xvel = 0;
+ b->yvel = 0;
+ b->rem = 0;
+ b->txrrow = 8;
+ b->txrcol = id;
+ b->anim = 4;
+ b->rate = 150 + (rand() % 50 - 25);
+ b->collision_act = gribtake;
+ b->msg = 0;
+ b->msglen = 0;
+ } else if (type == '^') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->walls[i][j] = malloc(sizeof(tt_body));
+ tt_body *b = r->walls[i][j];
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->xvel = 0;
+ b->yvel = 0;
+ b->rem = 0;
+ b->txrrow = 8;
+ b->txrcol = id;
+ b->anim = 4;
+ b->rate = 150 + (rand() % 50 - 25);
+ b->collision_act = do_nothing;
+ b->msg = 0;
+ b->msglen = 0;
+ } else if (type == '&') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->walls[i][j] = malloc(sizeof(tt_body));
+ tt_body *b = r->walls[i][j];
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->xvel = 0;
+ b->yvel = 0;
+ b->rem = 0;
+ b->txrrow = 10;
+ b->txrcol = id;
+ b->anim = 1;
+ b->rate = 100;
+ b->collision_act = do_nothing;
+ b->msg = 0;
+ b->msglen = 0;
+ } else if (type == '.') {
+ r->floor[i][j] = newtile(0, c == ' ' ? default_floor_id : id);
+ } else if (type == '*') {
+ r->walls[i][j] = malloc(sizeof(tt_body));
+ tt_body *b = r->walls[i][j];
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->xvel = 0;
+ b->yvel = 0;
+ b->rem = 0;
+ b->txrrow = 9;
+ b->txrcol = id;
+ b->anim = 4;
+ b->rate = 150 + (rand() % 50 - 25);
+ b->collision_act = do_nothing;
+ b->msg = 0;
+ b->msglen = 0;
+ } else if (type == 'X') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->roof[i][j] = newtile(5, id);
+ } else if (type == '=') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->bodies_count++;
+ r->bodies = realloc(r->bodies,
+ sizeof(tt_body) * r->bodies_count);
+ tt_body *b = r->bodies + r->bodies_count - 1;
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->rem = 0;
+ b->txrrow = 0;
+ b->txrcol = 15;
+ b->anim = 1;
+ b->rate = 100;
+ b->collision_act = do_nothing;
+ } else if (type == '$') {
+ r->floor[i][j] = newtile(0, default_floor_id);
+ r->bodies_count++;
+ r->bodies = realloc(r->bodies,
+ sizeof(tt_body) * r->bodies_count);
+ tt_body *b = r->bodies + r->bodies_count - 1;
+ b->x = j * 32;
+ b->y = i * 32;
+ b->xrem = 0;
+ b->yrem = 0;
+ b->rem = 0;
+ b->txrrow = 7;
+ b->txrcol = id;
+ b->anim = 4;
+ b->rate = 100 + (rand() % 50 - 25);
+ b->collision_act = 0; /* todo: kill */
+ }
+ }
+ fgetc(f);
+ }
+ for (i = 0; i != r->bodies_count; ++i) {
+ r->bodies[i].yvel = readnum(f);
+ r->bodies[i].xvel = readnum(f);
+ r->bodies[i].msg = 0;
+ r->bodies[i].msglen = 0;
+ if (fgetc(f) == '=') {
+ int c = fgetc(f);
+ while (c != EOF && c != '\n') {
+ r->bodies[i].msglen++;
+ r->bodies[i].msg = realloc(r->bodies[i].msg,
+ r->bodies[i].msglen + 1);
+ r->bodies[i].msg[r->bodies[i].msglen - 1] = c;
+ c = fgetc(f);
+ }
+ r->bodies[i].msg[r->bodies[i].msglen] = 0;
+ }
+ }
+}
+
+void tt_map_load()
+{
+ char path[] = "data/map/0";
+ char r = '0';
+ while (r != '~') {
+ path[9] = r;
+ FILE *roomf = fopen(path, "r");
+ if (roomf) {
+ loadroom(ttmap + r, roomf);
+ fclose(roomf);
+ }
+ ++r;
+ }
+}
+
+
+void tt_map_free()
+{
+ return;
+}
diff --git a/src/map.h b/src/map.h
new file mode 100644
index 0000000..e8da22d
--- /dev/null
+++ b/src/map.h
@@ -0,0 +1,18 @@
+#ifndef TAKETHIS_INCLUDED_MAP
+#define TAKETHIS_INCLUDED_MAP
+
+
+/* room is identified by single char
+ * they are located in data/map/
+ * roomfile has four neighbors at the first line
+ * after this indexed tiles go
+ * after them each line is speed of corresponding enemy in percents
+ * and optionaly after = message of this enemy */
+
+
+void tt_map_load();
+
+void tt_map_free();
+
+
+#endif
diff --git a/src/player.c b/src/player.c
new file mode 100644
index 0000000..10d5c5f
--- /dev/null
+++ b/src/player.c
@@ -0,0 +1,61 @@
+#include "player.h"
+
+#include "globals.h"
+
+
+void tt_player_draw()
+{
+ int i, j;
+ tt_room *r = ttplayer.room;
+ for (i = 0; i != TT_ROOM_H; ++i) {
+ for (j = 0; j != TT_ROOM_W; ++j) {
+ SDL_Rect d = { 14 + j * 32, 14 + i * 32, 32, 32 };
+ SDL_Rect *f = r->floor[i][j];
+ tt_body *w = r->walls[i][j];
+ if (f) SDL_RenderCopy(ttrdr, tttxr, f, &d);
+ if (w) {
+ SDL_Rect s = {
+ (w->txrcol * w->anim + w->rem / w->rate % w->anim) * 16,
+ w->txrrow * 16, 16, 16
+ };
+ SDL_RenderCopy(ttrdr, tttxr, &s, &d);
+ }
+ }
+ }
+ for (i = 0; i != r->bodies_count; ++i) {
+ tt_body *b = r->bodies + i;
+ SDL_Rect s = {
+ (b->txrcol * b->anim + b->rem / b->rate % b->anim) * 16,
+ b->txrrow * 16, 16, 16
+ };
+ SDL_Rect d = { 14 + b->x, 14 + b->y, 32, 32 };
+ SDL_RenderCopy(ttrdr, tttxr, &s, &d);
+ if (b->msg) {
+ SDL_Color c = { 255, 255, 255, 255 };
+ SDL_Surface *s = TTF_RenderText_Blended(ttfont, b->msg, c);
+ SDL_Texture *t = SDL_CreateTextureFromSurface(ttrdr, s);
+ SDL_Rect dst = { 50 + b->x, 20 + b->y, s->w, s->h };
+ SDL_RenderCopy(ttrdr, t, 0, &dst);
+ SDL_DestroyTexture(t);
+ SDL_FreeSurface(s);
+ }
+ }
+
+ SDL_Rect d = { 14 + ttplayer.x, 14 + ttplayer.y, 32, 32 };
+ int dir = 0;
+ if (ttplayer.xwalk == 1) dir = 6;
+ else if (ttplayer.xwalk == -1) dir = 2;
+ else if (ttplayer.ywalk == -1) dir = 4;
+ SDL_Rect s = { 16 * (dir + (ttplayer.rem / 100 % 2)),
+ 16 * (5 + ttplayer.variant),
+ 16, 16 };
+ SDL_RenderCopy(ttrdr, tttxr, &s, &d);
+
+ for (i = 0; i != TT_ROOM_H; ++i) {
+ for (j = 0; j != TT_ROOM_W; ++j) {
+ SDL_Rect d = { 14 + j * 32, 14 + i * 32, 32, 32 };
+ SDL_Rect *roof = r->roof[i][j];
+ if (roof) SDL_RenderCopy(ttrdr, tttxr, roof, &d);
+ }
+ }
+}
diff --git a/src/player.h b/src/player.h
index 427c9d3..cc224ba 100644
--- a/src/player.h
+++ b/src/player.h
@@ -1,20 +1,29 @@
-#ifndef TAKETHIS_INCLUDED_ROOM
-#define TAKETHIS_INCLUDED_ROOM
+#ifndef TAKETHIS_INCLUDED_PLAYER
+#define TAKETHIS_INCLUDED_PLAYER
-typedef struct tt_room {
- SDL_Rect *tiles[15][19];
- tt_body *bodies[15][19];
-} tt_room;
+#include "room.h"
-tt_room *tt_room_load(int num);
+typedef struct tt_player {
+ tt_room *room;
+ int xwalk;
+ int ywalk;
+ int x;
+ int y;
+ int xrem;
+ int yrem;
+ int variant;
+ int rem;
+ int money;
+ int keys;
+ int the_key;
+} tt_player;
-void tt_room_draw(tt_room *room);
-void tt_room_rtol(tt_room *right, tt_room *left, int permille);
+void tt_player_walk(int delta);
-void tt_room_utod(tt_room *up, tt_room *down, int permille);
+void tt_player_draw();
#endif
diff --git a/src/room.c b/src/room.c
index 3d8d6c0..b45ada8 100644
--- a/src/room.c
+++ b/src/room.c
@@ -1,60 +1,27 @@
#include "room.h"
-#include <stdio.h>
-#include "texture.h"
-
-static SDL_Rect *rect(int x, int y, int w, int h)
-{
- SDL_Rect *res = malloc(sizeof(*res));
- res->x = x;
- res->y = y;
- res->w = w;
- res->h = h;
- return res;
-}
-
-tt_room *tt_room_load(char c)
+int tt_room_collide(tt_room *room, SDL_Rect *box)
{
- tt_room *res = malloc(sizeof(*res));
- char path[] = "data/rooms/0";
- path[11] = c;
- FILE *f = fopen(path, "r");
+ SDL_Rect r = { 0, 0, 32, 32 };
int i, j;
for (i = 0; i != TT_ROOM_H; ++i) {
+ r.y = 32 * i;
for (j = 0; j != TT_ROOM_W; ++j) {
- int type = fgetc(f);
- res->tiletypes[i][j] = type;
- res->bodies[i][j] = 0;
- int id = fgetc(f);
- if (id == ' ') id = '0';
- if (id < '0' || '9' < id) goto error;
- id = (id - '0') * 16;
- if (type == '.') res->tiles[i][j] = rect(id, 16, 16, 16);
- else if (type == '#') res->tiles[i][j] = rect(id, 0, 16, 16);
- else goto error;
+ r.x = 32 * j;
+ if (SDL_HasIntersection(box, room->walls[i][j] ? &r : 0))
+ return 1;
}
- fgetc(f);
}
- fclose(f);
- return res;
-error:
- fclose(f);
- free(res);
return 0;
}
-
-void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room)
+int tt_room_out(tt_room *room, SDL_Rect *box)
{
- int i, j;
- for (i = 0; i != TT_ROOM_H; ++i) {
- for (j = 0; j != TT_ROOM_W; ++j) {
- const SDL_Rect *src = room->tiles[i][j];
- if (src) {
- SDL_Rect dst = { 30 + 32 * j, 30 + 32 * i, 32, 32 };
- SDL_RenderCopy(rdr, txr, src, &dst);
- }
- }
- }
+ if (!box) return 0;
+ if (box->y < 0) return 1;
+ if (box->x + box->w > TT_ROOM_W * 32) return 2;
+ if (box->y + box->h > TT_ROOM_H * 32) return 3;
+ if (box->x < 0) return 4;
+ return 0;
}
diff --git a/src/room.h b/src/room.h
index 9d6661a..b4567f2 100644
--- a/src/room.h
+++ b/src/room.h
@@ -3,29 +3,28 @@
#include <SDL2/SDL.h>
+#include "body.h"
-#define TT_ROOM_W 19
-#define TT_ROOM_H 15
+#define TT_ROOM_W 20
+#define TT_ROOM_H 16
typedef struct tt_room {
- char tiletypes[TT_ROOM_H][TT_ROOM_W];
- const SDL_Rect *tiles[TT_ROOM_H][TT_ROOM_W];
- void *bodies[TT_ROOM_H][TT_ROOM_W];
+ SDL_Rect *floor[TT_ROOM_H][TT_ROOM_W];
+ tt_body *walls[TT_ROOM_H][TT_ROOM_W];
+ SDL_Rect *roof[TT_ROOM_H][TT_ROOM_W];
+ tt_body *bodies;
+ int bodies_count;
+ struct tt_room *neighbours[4];
} tt_room;
-tt_room *tt_room_load(char c);
+void tt_room_draw_background(tt_room *room);
+void tt_room_draw_foreground(tt_room *room);
-void tt_room_draw(SDL_Renderer *rdr, SDL_Texture *txr, tt_room *room);
+int tt_room_collide(tt_room *room, SDL_Rect *box);
-/*
-void tt_room_rtol(SDL_Renderer *rdr, SDL_Texture *txr,
- tt_room *right, tt_room *left, int permille);
-
-void tt_room_utod(SDL_Renderer *rdr, SDL_Texture *txr,
- tt_room *up, tt_room *down, int permille);
-*/
+int tt_room_out(tt_room *room, SDL_Rect *box);
#endif
diff --git a/src/texture.c b/src/texture.c
deleted file mode 100644
index e046f6f..0000000
--- a/src/texture.c
+++ /dev/null
@@ -1,22 +0,0 @@
-#include "texture.h"
-
-
-const SDL_Rect tt_texture_floor = { 16 * 7, 0, 16, 16 };
-const SDL_Rect tt_texture_corner_lu = { 16 * 5, 0, 16, 16 };
-const SDL_Rect tt_texture_corner_ru = { 16 * 6, 0, 16, 16 };
-const SDL_Rect tt_texture_corner_ld = { 16 * 5, 16, 16, 16 };
-const SDL_Rect tt_texture_corner_rd = { 16 * 6, 16, 16, 16 };
-const SDL_Rect tt_texture_wall_l = { 16 * 8, 0, 16, 16 };
-const SDL_Rect tt_texture_wall_r = { 16 * 8, 16, 16, 16 };
-const SDL_Rect tt_texture_wall_u = { 16 * 9, 0, 16, 16 };
-const SDL_Rect tt_texture_wall_d = { 16 * 9, 16, 16, 16 };
-const SDL_Rect tt_texture_player = { 0, 0, 0, 0 };
-
-
-SDL_Texture *tt_texture_load(SDL_Renderer *rdr)
-{
- SDL_Surface *surf = SDL_LoadBMP("data/tiles.bmp");
- SDL_Texture *res = SDL_CreateTextureFromSurface(rdr, surf);
- SDL_FreeSurface(surf);
- return res;
-}
diff --git a/src/texture.h b/src/texture.h
deleted file mode 100644
index 48ecdeb..0000000
--- a/src/texture.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef TAKETHIS_INCLUDED_TEXTURE
-#define TAKETHIS_INCLUDED_TEXTURE
-
-
-#include <SDL2/SDL.h>
-
-
-extern const SDL_Rect tt_texture_floor;
-extern const SDL_Rect tt_texture_corner_lu;
-extern const SDL_Rect tt_texture_corner_ru;
-extern const SDL_Rect tt_texture_corner_ld;
-extern const SDL_Rect tt_texture_corner_rd;
-extern const SDL_Rect tt_texture_wall_l;
-extern const SDL_Rect tt_texture_wall_r;
-extern const SDL_Rect tt_texture_wall_u;
-extern const SDL_Rect tt_texture_wall_d;
-extern const SDL_Rect tt_texture_player;
-
-
-SDL_Texture *tt_texture_load(SDL_Renderer *rdr);
-
-
-#endif