Browse Source

Initial Commit

Rick Novile 2 years ago
commit
3775e92275
38 changed files with 524 additions and 0 deletions
  1. BIN
      assets/black mino.png
  2. BIN
      assets/blue mino.png
  3. BIN
      assets/cyan mino.png
  4. BIN
      assets/green mino.png
  5. BIN
      assets/grey mino.png
  6. BIN
      assets/mino base.png
  7. BIN
      assets/orange mino.png
  8. BIN
      assets/playfield.png
  9. BIN
      assets/purple mino.png
  10. BIN
      assets/red mino.png
  11. BIN
      assets/white mino.png
  12. BIN
      assets/yellow mino.png
  13. BIN
      bin/bag.o
  14. BIN
      bin/draw.o
  15. BIN
      bin/init.o
  16. BIN
      bin/input.o
  17. BIN
      bin/main.o
  18. BIN
      bin/matrix.o
  19. BIN
      bin/piece.o
  20. BIN
      bin/playfield.o
  21. 29
    0
      makefile
  22. 32
    0
      src/bag.c
  23. 14
    0
      src/bag.h
  24. 52
    0
      src/draw.c
  25. 16
    0
      src/draw.h
  26. 32
    0
      src/init.c
  27. 10
    0
      src/init.h
  28. 50
    0
      src/input.c
  29. 9
    0
      src/input.h
  30. 123
    0
      src/main.c
  31. 6
    0
      src/matrix.c
  32. 15
    0
      src/matrix.h
  33. 56
    0
      src/piece.c
  34. 16
    0
      src/piece.h
  35. 32
    0
      src/playfield.c
  36. 21
    0
      src/playfield.h
  37. 11
    0
      src/structs.h
  38. BIN
      tivien

BIN
assets/black mino.png View File


BIN
assets/blue mino.png View File


BIN
assets/cyan mino.png View File


BIN
assets/green mino.png View File


BIN
assets/grey mino.png View File


BIN
assets/mino base.png View File


BIN
assets/orange mino.png View File


BIN
assets/playfield.png View File


BIN
assets/purple mino.png View File


BIN
assets/red mino.png View File


BIN
assets/white mino.png View File


BIN
assets/yellow mino.png View File


BIN
bin/bag.o View File


BIN
bin/draw.o View File


BIN
bin/init.o View File


BIN
bin/input.o View File


BIN
bin/main.o View File


BIN
bin/matrix.o View File


BIN
bin/piece.o View File


BIN
bin/playfield.o View File


+ 29
- 0
makefile View File

@@ -0,0 +1,29 @@
1
+CC = gcc
2
+OUT = bin
3
+PROG = tivien
4
+SEARCHPATH += src 
5
+
6
+CFLAGS += `sdl2-config --cflags` -g -lefence 
7
+LDFLAGS += `sdl2-config --libs` -lSDL2_mixer -lSDL2_ttf -lSDL2_image -lm 
8
+
9
+vpath %.c $(SEARCHPATH)
10
+vpath %.h $(SEARCHPATH)
11
+
12
+_OBJS += main.o
13
+_OBJS += bag.o matrix.o piece.o  playfield.o init.o draw.o input.o
14
+
15
+OBJS = $(patsubst %,$(OUT)/%,$(_OBJS))
16
+
17
+all: $(PROG)
18
+
19
+$(PROG): $(OBJS)
20
+	$(CC) -o $@ $(OBJS) $(LDFLAGS)
21
+
22
+$(OUT)/%.o: %.c
23
+	@mkdir -p $(OUT)
24
+	$(CC) $(CFLAGS) -c -o $@ $<
25
+
26
+.PHONY: clean
27
+
28
+clean:
29
+	$(RM) -rf $(OUT) $(PROG)

+ 32
- 0
src/bag.c View File

@@ -0,0 +1,32 @@
1
+#include <string.h>
2
+#include <stdlib.h>
3
+#include <time.h>
4
+#include "bag.h"
5
+
6
+void bag_init(Bag *bag, Piece* pieces, int size) {
7
+    bag->size = size;
8
+    bag->max = size;
9
+    bag->pieces = malloc(sizeof(Piece) * bag->size);
10
+    memcpy(bag->pieces, pieces, sizeof(Piece) * bag->size);
11
+}
12
+
13
+Piece bag_pop(Bag *bag) {
14
+    Piece output = bag->pieces[bag->size - 1];
15
+    if(bag->size == 1) {
16
+        bag->size = bag->max;
17
+        bag_randomize(bag);
18
+    } else {
19
+        bag->size--;
20
+    }
21
+    return output;
22
+}
23
+
24
+void bag_randomize(Bag *bag) {
25
+    Piece* pieces = bag->pieces;
26
+    for(int i = 0; i < bag->size - 1; i++) {
27
+        size_t j = i + rand() / (RAND_MAX / (bag->size - i) + 1);
28
+        Piece t = bag->pieces[j];
29
+        bag->pieces[j] = bag->pieces[i];
30
+        bag->pieces[i] = t;
31
+    }
32
+}

+ 14
- 0
src/bag.h View File

@@ -0,0 +1,14 @@
1
+#ifndef tivien_bag_h
2
+#define tivien_bag_h
3
+
4
+#include "piece.h"
5
+
6
+typedef struct {
7
+    int max, size;
8
+    Piece* pieces;
9
+} Bag;
10
+
11
+void bag_init(Bag *bag, Piece* pieces, int size);
12
+Piece bag_pop(Bag *bag);
13
+
14
+#endif

+ 52
- 0
src/draw.c View File

@@ -0,0 +1,52 @@
1
+#include "draw.h"
2
+
3
+void clear_scene(SDL_Renderer* renderer) {
4
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
5
+    SDL_RenderClear(renderer);
6
+}
7
+
8
+void render_scene(SDL_Renderer* renderer) {
9
+    SDL_RenderPresent(renderer);
10
+}
11
+
12
+SDL_Texture *load_texture(SDL_Renderer* renderer, char* filepath) {
13
+    SDL_Texture *texture;
14
+    texture = IMG_LoadTexture(renderer, filepath);
15
+    return texture;
16
+}
17
+
18
+void draw_texture(SDL_Renderer* renderer, SDL_Texture *texture, int x, int y, int w, int h) {
19
+	SDL_Rect pos;
20
+	pos.x = x;
21
+	pos.y = y;
22
+	pos.w = w;
23
+	pos.h = h;
24
+	SDL_RenderCopy(renderer, texture, NULL, &pos);
25
+}
26
+
27
+SDL_Texture *get_block_texture(SDL_Renderer* renderer, char* color) {
28
+	if(strcmp(color, "blue") == 0) {
29
+		return load_texture(renderer, "./assets/blue mino.png");
30
+	} 
31
+	else if(strcmp(color, "cyan") == 0) {
32
+		return load_texture(renderer, "./assets/cyan mino.png");
33
+	}
34
+	else if(strcmp(color, "red") == 0) {
35
+		return load_texture(renderer, "./assets/red mino.png");
36
+	}
37
+	else if(strcmp(color, "green") == 0) {
38
+		return load_texture(renderer, "./assets/green mino.png");
39
+	}
40
+	else if(strcmp(color, "purple") == 0) {
41
+		return load_texture(renderer, "./assets/purple mino.png");
42
+	}
43
+	else if(strcmp(color, "orange") == 0) {
44
+		return load_texture(renderer, "./assets/orange mino.png");
45
+	}
46
+	else if(strcmp(color, "yellow") == 0) {
47
+		return load_texture(renderer, "./assets/yellow mino.png");
48
+	}
49
+	else {
50
+		return load_texture(renderer, "./assets/grey mino.png");
51
+	}
52
+}

+ 16
- 0
src/draw.h View File

@@ -0,0 +1,16 @@
1
+#include <stdlib.h>
2
+#include "SDL2/SDL.h"
3
+#include "SDL2/SDL_image.h"
4
+#include "structs.h"
5
+
6
+#ifndef tivien_draw_h
7
+#define tivien_draw_h
8
+
9
+void clear_scene(SDL_Renderer* renderer);
10
+void render_scene(SDL_Renderer* renderer);
11
+SDL_Texture *load_texture(SDL_Renderer* renderer, char* filepath);
12
+void draw_texture(SDL_Renderer* renderer, SDL_Texture *texture, int x, int y, int w, int h);
13
+SDL_Texture *get_block_texture(SDL_Renderer* renderer, char* color);
14
+
15
+
16
+#endif

+ 32
- 0
src/init.c View File

@@ -0,0 +1,32 @@
1
+#include "init.h"
2
+
3
+void init_SDL(Context* context) {
4
+    SDL_Init(SDL_INIT_VIDEO);
5
+
6
+    context->window = SDL_CreateWindow(
7
+        "Tivien - Gamma 0.0.0",
8
+        SDL_WINDOWPOS_UNDEFINED,
9
+        SDL_WINDOWPOS_UNDEFINED,
10
+        432,
11
+        792,
12
+        0
13
+    );
14
+
15
+    if(context->window == NULL) {
16
+        printf("Could not start SDL Window: %s\n", SDL_GetError());
17
+        exit(1);
18
+    }
19
+
20
+    context->renderer = SDL_CreateRenderer(context->window, -1, SDL_RENDERER_ACCELERATED);
21
+
22
+    if(context->renderer == NULL) {
23
+        printf("Could not start SDL Renderer: %s\n", SDL_GetError());
24
+        exit(1);
25
+    }
26
+}
27
+
28
+void end_SDL(Context context) {
29
+    SDL_DestroyRenderer(context.renderer);
30
+    SDL_DestroyWindow(context.window);
31
+    SDL_Quit();
32
+}

+ 10
- 0
src/init.h View File

@@ -0,0 +1,10 @@
1
+#include "SDL2/SDL.h"
2
+#include "structs.h"
3
+
4
+#ifndef tivien_init_h
5
+#define tivien_init_h
6
+
7
+void init_SDL(Context* context);
8
+void end_SDL(Context context);
9
+
10
+#endif

+ 50
- 0
src/input.c View File

@@ -0,0 +1,50 @@
1
+#include "input.h"
2
+
3
+void parse_key_press(SDL_KeyboardEvent *event) {
4
+    if(event->repeat == 0) {
5
+        switch(event->keysym.scancode) {
6
+            case SDL_SCANCODE_UP:
7
+                break;
8
+
9
+            case SDL_SCANCODE_DOWN:
10
+                break;
11
+
12
+            case SDL_SCANCODE_LEFT:
13
+                break;
14
+
15
+            case SDL_SCANCODE_RIGHT:
16
+                break;
17
+
18
+            case SDL_SCANCODE_Z:
19
+                break;
20
+
21
+            case SDL_SCANCODE_X:
22
+                break;
23
+
24
+            case SDL_SCANCODE_SPACE:
25
+                break;
26
+
27
+            default:
28
+                break;
29
+        }
30
+    }
31
+}
32
+
33
+void parse_input() {
34
+    SDL_Event event;
35
+
36
+    while(SDL_PollEvent(&event)) {
37
+        switch(event.type) {
38
+            case SDL_QUIT:
39
+                exit(0);
40
+                break;
41
+
42
+            case SDL_KEYDOWN:
43
+                parse_key_press(&event.key);
44
+                break;
45
+
46
+            default:
47
+                break;
48
+        }
49
+    }
50
+}

+ 9
- 0
src/input.h View File

@@ -0,0 +1,9 @@
1
+#include "SDL2/SDL.h"
2
+
3
+#ifndef tivien_input_h
4
+#define tivien_input_h
5
+
6
+void parse_key_press(SDL_KeyboardEvent* event);
7
+void parse_input();
8
+
9
+#endif

+ 123
- 0
src/main.c View File

@@ -0,0 +1,123 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <time.h>
4
+#include "matrix.h"
5
+#include "piece.h"
6
+#include "bag.h"
7
+#include "playfield.h"
8
+#include "draw.h"
9
+#include "structs.h"
10
+#include "init.h"
11
+#include "input.h"
12
+
13
+int main(int argv, char** argc) {
14
+    srand(time(NULL));
15
+    Context context;
16
+    memset(&context, 0, sizeof(Context));
17
+    init_SDL(&context);
18
+    SDL_Texture *playfield;
19
+    playfield = load_texture(context.renderer, "./assets/playfield.png");
20
+    while(1) {
21
+        clear_scene(context.renderer);
22
+        parse_input();
23
+        draw_texture(context.renderer, playfield, 0, 0, 432, 792);
24
+        render_scene(context.renderer);
25
+        SDL_Delay(16);
26
+    }
27
+    return 0;
28
+}
29
+
30
+
31
+/* useless test code
32
+
33
+    int t4a[4][3][3] = {
34
+        {
35
+            {0, 1, 0},
36
+            {1, 1, 1},
37
+            {0, 0, 0}
38
+        },
39
+        {
40
+            {0, 1, 0},
41
+            {0, 1, 1},
42
+            {0, 1, 0}
43
+        },
44
+        {
45
+            {0, 0, 0},
46
+            {1, 1, 1},
47
+            {0, 1, 0}
48
+        },
49
+        {
50
+            {0, 1, 0},
51
+            {1, 1, 0},
52
+            {0, 1, 0}
53
+        }
54
+    };
55
+
56
+    int i4a[4][4][4] = {
57
+        {
58
+            {0, 0, 0, 0},
59
+            {1, 1, 1, 1},
60
+            {0, 0, 0, 0},
61
+            {0, 0, 0, 0}
62
+        },
63
+        {
64
+            {0, 0, 1, 0},
65
+            {0, 0, 1, 0},
66
+            {0, 0, 1, 0},
67
+            {0, 0, 1, 0}
68
+        },
69
+        {
70
+            {0, 0, 0, 0},
71
+            {0, 0, 0, 0}, 
72
+            {1, 1, 1, 1}, 
73
+            {0, 0, 0, 0}
74
+        },
75
+        {
76
+            {0, 1, 0, 0},
77
+            {0, 1, 0, 0},
78
+            {0, 1, 0, 0},
79
+            {0, 1, 0, 0}
80
+        }
81
+    };
82
+    
83
+    Matrix i4m, t4m;
84
+    Piece i4, t4;
85
+    Bag bag;
86
+    piece_and_matrix_init(
87
+        &i4,
88
+        3,
89
+        19,
90
+        0,
91
+        4,
92
+        4,
93
+        "cyan",
94
+        &i4m,
95
+        i4a
96
+    );
97
+    piece_and_matrix_init(
98
+        &t4,
99
+        4,
100
+        19,
101
+        0,
102
+        3,
103
+        4,
104
+        "purple",
105
+        &t4m,
106
+        t4a
107
+    );
108
+    Piece array[3] = {
109
+        i4, i4, t4
110
+    };
111
+    bag_init(&bag, array, 3);
112
+    Playfield playfield;
113
+    init_playfield(&playfield);
114
+    playfield.data.std[19][5] = 1;
115
+    char* test = bag.pieces[1].color;
116
+    Piece new = bag_pop(&bag);
117
+    Piece new2 = bag_pop(&bag);
118
+    Piece new3 = bag_pop(&bag);
119
+    Piece new4 = bag_pop(&bag);
120
+    Piece new5 = bag_pop(&bag);
121
+    printf("%i\n", check_collision(playfield, t4, -1, 0));
122
+
123
+    */

+ 6
- 0
src/matrix.c View File

@@ -0,0 +1,6 @@
1
+#include <string.h>
2
+#include "matrix.h"
3
+
4
+void matrix_init(Matrix *matrix, int*** array, int count, int size) {
5
+    memcpy(matrix, array, sizeof(int[count][size][size]));
6
+}

+ 15
- 0
src/matrix.h View File

@@ -0,0 +1,15 @@
1
+#ifndef tivien_matrix_h
2
+#define tivien_matrix_h
3
+
4
+typedef union {
5
+    int m11[1][1][1];
6
+    int m12[1][2][2];
7
+    int m42[4][2][2];
8
+    int m23[2][3][3];
9
+    int m43[4][3][3];
10
+    int m44[4][4][4];
11
+} Matrix;
12
+
13
+void matrix_init(Matrix *matrix, int*** array, int count, int size);
14
+
15
+#endif

+ 56
- 0
src/piece.c View File

@@ -0,0 +1,56 @@
1
+#include "piece.h"
2
+
3
+void piece_init(Piece *piece, int x, int y, int rotation, int size, int states, char* color, Matrix matrix) {
4
+    piece->x = x;
5
+    piece->y = y;
6
+    piece->rotation = rotation;
7
+    piece->size = size;
8
+    piece->states = states;
9
+    piece->color = color;
10
+    piece->matrix = matrix;
11
+}
12
+
13
+void piece_and_matrix_init(Piece *piece, int x, int y, int rotation, int size, int states, char* color, Matrix *matrix, int*** array) {
14
+    matrix_init(matrix, array, states, size);
15
+    piece_init(piece, x, y, rotation, size, states, color, *matrix);
16
+}
17
+
18
+int access_matrix(Piece piece, int state, int x, int  y) {
19
+    switch(piece.states) {
20
+        case 1:
21
+            if(piece.size == 1) {
22
+                return piece.matrix.m11[state][y][x];
23
+                break;
24
+            }
25
+            return piece.matrix.m12[state][y][x];
26
+            break;
27
+
28
+        case 2:
29
+            return piece.matrix.m23[state][y][x];
30
+            break;
31
+
32
+        case 4:
33
+            switch(piece.size) {
34
+                case 2:
35
+                    return piece.matrix.m42[state][y][x];
36
+                    break;
37
+                
38
+                case 3:
39
+                    return piece.matrix.m43[state][y][x];
40
+                    break;
41
+
42
+                case 4:
43
+                    return piece.matrix.m44[state][y][x];
44
+                    break;
45
+
46
+                default:
47
+                    return 0;
48
+                    break;
49
+            }
50
+            break;
51
+        
52
+        default:
53
+            return 0;
54
+            break;
55
+    }
56
+}

+ 16
- 0
src/piece.h View File

@@ -0,0 +1,16 @@
1
+#ifndef tivien_piece_h
2
+#define tivien_piece_h
3
+
4
+#include "matrix.h"
5
+
6
+typedef struct {
7
+    int x, y, rotation, size, states;
8
+    char* color;
9
+    Matrix matrix;
10
+} Piece;
11
+
12
+void piece_init(Piece *piece, int x, int y, int rotation, int size, int states, char* color, Matrix matrix);
13
+void piece_and_matrix_init(Piece *piece, int x, int y, int rotation, int size, int states, char* color, Matrix *matrix, int*** array);
14
+int access_matrix(Piece piece, int state, int x, int  y);
15
+
16
+#endif

+ 32
- 0
src/playfield.c View File

@@ -0,0 +1,32 @@
1
+#include "playfield.h"
2
+
3
+//TODO: Add variable size playfield support
4
+void init_playfield(Playfield *playfield/*, int width, int height*/) {
5
+    playfield->width = 10;
6
+    playfield->height = 20;
7
+    for(int i = 0; i < playfield->height; i++) {
8
+        for(int j = 0; j < playfield->width; j++) {
9
+            playfield->data.std[i][j] = 0;
10
+        }
11
+    }
12
+    //playfield->data = malloc(sizeof(int) * playfield->width * playfield->height);
13
+}
14
+
15
+int check_collision(Playfield playfield, Piece piece, int x_offset, int y_offset) {
16
+    int pfx = piece.x + x_offset;
17
+    int pfy = piece.y + y_offset;
18
+    for(int v = 0; v < piece.size; v++) {
19
+        for(int h = 0; h < piece.size; h++) {
20
+            int cx = pfx + h;
21
+            int cy = pfy - v;
22
+            if(
23
+                (cx < 0 || cy < 0 ||
24
+                cx > playfield.width || cy > playfield.height ||
25
+                playfield.data.std[cy][cx] >= 1) && (access_matrix(piece, piece.rotation, h, v) == 1)
26
+            ) {
27
+                return 1;
28
+            }
29
+        }
30
+    }
31
+    return 0;
32
+}

+ 21
- 0
src/playfield.h View File

@@ -0,0 +1,21 @@
1
+#include <stdlib.h>
2
+#include "piece.h"
3
+
4
+#ifndef tivien_playfield_h
5
+#define tivien_playfield_h
6
+
7
+
8
+typedef union {
9
+    int std[20][10];
10
+} PlayfieldMatrix;
11
+
12
+
13
+typedef struct {
14
+    int width, height;
15
+    PlayfieldMatrix data;
16
+} Playfield;
17
+
18
+void init_playfield(Playfield *playfield);
19
+int check_collision(Playfield playfield, Piece piece, int x_offset, int y_offset);
20
+
21
+#endif

+ 11
- 0
src/structs.h View File

@@ -0,0 +1,11 @@
1
+#include "SDL2/SDL.h"
2
+
3
+#ifndef tivien_structs_h
4
+#define tivien_structs_h
5
+
6
+typedef struct {
7
+    SDL_Renderer* renderer;
8
+    SDL_Window* window;
9
+} Context;
10
+
11
+#endif

BIN
tivien View File


Loading…
Cancel
Save