Initial commit

This commit is contained in:
Alexandre 2024-08-18 16:04:22 +02:00
commit a73fa17a12
32 changed files with 719 additions and 0 deletions

11
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"files.associations": {
"stdio.h": "c",
"consts.h": "c",
"math.h": "c",
"sdl_image.h": "c",
"display.h": "c",
"base.h": "c",
"assembly.h": "c"
}
}

29
Makefile Normal file
View File

@ -0,0 +1,29 @@
CC = gcc
FLAGS = -Wall -Wextra -Wpedantic -g
LFLAGS = -lSDL2 -lSDL2_image -lm -lncurses
all: bin/mamaker
test: bin/mamaker
bin/mamaker
bin/mamaker: obj/main.o obj/base.o obj/assembly.o obj/display.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
obj/%.o: src/%.c
@mkdir -p obj
$(CC) -o $@ -c $(FLAGS) $<
obj/main.o: src/main.c
obj/base.o: src/base.c
obj/assembly.o: src/assembly.c
obj/display.o: src/display.c
.PHONY: clean mrproper
clean:
rm -rf obj/
mrproper: clean
rm -rf bin/

1
README.md Normal file
View File

@ -0,0 +1 @@
Ever wished you could make your own mario party board ?

BIN
bin/mamaker Executable file

Binary file not shown.

BIN
obj/assembly.o Normal file

Binary file not shown.

BIN
obj/base.o Normal file

Binary file not shown.

BIN
obj/display.o Normal file

Binary file not shown.

BIN
obj/main.o Normal file

Binary file not shown.

BIN
res/digits/big-sign-sum.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

BIN
res/digits/digit-0.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-1.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-2.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-3.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-4.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-5.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-6.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-7.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-8.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-9.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-minus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-plus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-prod.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sum-k.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

162
src/assembly.c Normal file
View File

@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "consts.h"
#include "base.h"
#include "assembly.h"
void init_empty_board() {
start = malloc(sizeof(cell));
start->id = 0 ;
start->coord_x = 50 ;
start->coord_y = 50 ;
start->star_allowed = false ;
start->type = START ;
start->prev_1 = start;
start->prev_2 = NULL;
start->prev_3 = NULL;
start->prev_4 = NULL;
start->prev_5 = NULL;
start->next_1 = start;
start->next_2 = NULL;
start->next_3 = NULL;
start->next_4 = NULL;
start->next_5 = NULL;
start->meta = "start";
}
void init_players() {
players = malloc(sizeof(player)*n_players);
for(int i = 0; i < n_players; i++) {
players[i].name = "player" ;
players[i].standing_tile = start ;
players[i].coins = 10 ;
players[i].stars = 0 ;
players[i].items = malloc(sizeof(ITEM)*3) ;
players[i].max_items = 3 ;
players[i].item_len = 0 ;
players[i].stats.minigames_wins = 0;
players[i].stats.coin_collected = 0 ;
players[i].stats.red_count = 0 ;
players[i].stats.bowser_count = 0 ;
players[i].stats.items_used = 0 ;
players[i].stats.lost_coins = 0 ;
players[i].stats.lost_stars = 0 ;
players[i].stats.tiles_travelled = 0 ;
players[i].stats.events_triggered = 0 ;
players[i].stats.hidden_blocks = 0 ;
}
}
void init_shop(char* filename) {
item_costs = malloc(sizeof(int)*n_items);
item_start = malloc(sizeof(int)*n_items) ;
item_end = malloc(sizeof(int)*n_items) ;
item_is_normal_type = malloc(sizeof(bool)*n_items) ;
item_is_special_type = malloc(sizeof(bool)*n_items) ;
FILE* ptr = fopen(filename, "r");
if(ptr == NULL) {
fprintf(stderr, "ERROR : unable to load config file %s\n", filename);
exit(1);
} else {
printf(" Loading %s...\n", filename);
}
int buffer = -1 ;
int index = 0 ;
int sub_index = 0 ;
char c = fgetc(ptr);
bool is_reading = false ;
bool ignore_line = false ;
while(c != EOF && c != '$') {
if(!ignore_line) {
if(c == '{') {
is_reading = true;
} else if(c == '}') {
is_reading = false;
sub_index = 0;
} else if(c == '*') {
ignore_line = true;
} else if(c == ':') {
buffer = 0;
} else if(is_reading && buffer != -1) {
if(is_an_integer(c)) {
buffer *= 10;
buffer += (char)c - 48;
} else {
if(sub_index == 0) {
index = buffer;
} else if(sub_index == 1) {
item_start[index] = buffer;
} else if(sub_index == 2) {
item_end[index] = n_turns - buffer;
} else if(sub_index == 3) {
item_costs[index] = buffer;
} else if(sub_index == 4) {
item_is_normal_type[index] = (bool)buffer;
} else if(sub_index == 5) {
item_is_special_type[index] = (bool)buffer;
};
sub_index += 1;
buffer = -1;
}
}
};
if(c == '\n') {
ignore_line = false;
};
c = fgetc(ptr);
};
fclose(ptr);
}
void init_everything() {
printf("Initializing board...\n");
init_empty_board();
printf("Initializing players...\n");
init_players();
printf("Initializing items...\n");
init_shop("src/config.txt");
printf("Initialization complete...\n");
//print_items();
}
void print_items() {
for(int i = 0; i < n_items; i++) {
printf("Item : %d\n", i);
printf("cost : %d\nTurns : [%d - %d]\ntypes=[N = %d , S = %d]", item_costs[i], item_start[i], item_end[i], item_is_normal_type[i], item_is_special_type[i]);
printf("\n\n");
}
}
void destroy_everything() {
for(int i = 0; i < n_players; i++) {
free(players[i].items);
};
free(players);
free(item_costs);
free(item_start);
free(item_end);
free(item_is_normal_type);
free(item_is_special_type);
free(cell_count);
}
#include "consts.h"
#include "assembly.h"

16
src/assembly.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef GBM_ASSEMBLY_H
#define GBM_ASSEMBLY_H
void init_empty_board();
void init_players();
void init_shop(char* filename);
void init_everything();
void destroy_everything();
void print_items();
#endif

114
src/base.c Normal file
View File

@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "consts.h"
#include "base.h"
int ln_baseN(int n, int b) {
int r = 0;
while(n != 0) {
n = n / b;
r++;
}
return r;
}
int pw(int x, int n) {
if (n<0)
return 0;
if (n==0)
return 1;
if (n%2==0)
return pw(x*x, n/2);
return x*pw(x*x, n/2);
}
bool is_an_integer(char c) {
return ((int)c >= 48 && (int)c <= 57);
}
imgs import_digits(SDL_Renderer* renderer) {
imgs res;
res.arr = malloc(sizeof(SDL_Texture*)*10);
SDL_Texture* texture;
SDL_Surface* img;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-0.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[0] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-1.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[1] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-2.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[2] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-3.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[3] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-4.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[4] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-5.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[5] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-6.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[6] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-7.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[7] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-8.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[8] = texture;
// -------------------------------------------------------- //
img = SDL_LoadBMP("./res/digits/digit-9.bmp");
texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
res.arr[9] = texture;
return res;
}
void free_digits(imgs dgts) {
for(int i = 0; i < 10; i++) {
SDL_DestroyTexture(dgts.arr[i]);
}
free(dgts.arr);
}

14
src/base.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef GBM_BASE_H
#define GBM_BASE_H
int ln_baseN(int n, int b);
int pw(int x, int n);
imgs import_digits(SDL_Renderer* renderer);
void free_digits(imgs dgts);
bool is_an_integer(char c);
#endif

126
src/config.txt Normal file
View File

@ -0,0 +1,126 @@
{
id:0
*DOUBLE_DICE
start:0
end:0
cost:5
is_normal:1
is_special:0
};
{
id:1
*SPECIAL_DOUBLE_DICE
start:0
end:0
cost:10
is_normal:0
is_special:1
};
{
id:2
*TRIPLE_DICE
start:0
end:0
cost:15
is_normal:1
is_special:0
};
{
id:3
*SPECIAL_TRIPLE_DICE
start:0
end:0
cost:21
is_normal:0
is_special:1
};
{
id:4
*CURSED_DICE
start:0
end:0
cost:4
is_normal:1
is_special:1
};
{
id:5
*SMALL_CUSTOM_DICE
start:0
end:5
cost:5
is_normal:1
is_special:0
};
{
id:6
*CUSTOM_DICE
start:5
end:0
cost:15
is_normal:1
is_special:1
};
{
id:7
*BOO_BELL
start:7
end:0
cost:25
is_normal:1
is_special:1
};
{
id:8
*GOLD_PIPE
start:12
end:0
cost:28
is_normal:1
is_special:1
};
{
id:9
*KEY
start:1
end:0
cost:3
is_normal:1
is_special:0
};
{
id:10
*PLUNDER_CHEST
start:0
end:0
cost:20
is_normal:0
is_special:1
};
{
id:11
*WARP_BLOCK
start:0
end:0
cost:8
is_normal:1
is_special:1
};
{
id:12
*SPECIAL_WARP_BLOCK
start:10
end:0
cost:15
is_normal:0
is_special:1
}
$
item structure :
{
id: INT (order in enum)
start: INT (starting turn)
end: INT (item stop selling at turn N_TURNS - this_value)
cost: INT
is_normal: A (all), N (normal shop) or S (special shops)
}

103
src/consts.h Normal file
View File

@ -0,0 +1,103 @@
#ifndef GBM_CONSTS_H
#define GBM_CONSTS_H
typedef enum CELLTYPE {
START,
BLUE,
RED,
EVENT,
VS,
BOWSER,
CHANCE_TIME,
BOO,
STAR,
BANK
} CELLTYPE ;
static int n_cell_type = 10 ;
typedef enum ITEM {
DOUBLE_DICE,
SPECIAL_DOUBLE_DICE,
TRIPLE_DICE,
SPECIAL_TRIPLE_DICE,
CURSED_DICE,
SMALL_CUSTOM_DICE,
CUSTOM_DICE,
BOO_BELL,
GOLD_PIPE,
KEY,
PLUNDER_CHEST,
WARP_BLOCK,
SPECIAL_WARP_BLOCK
} ITEM ;
static int n_items = 13 ;
typedef struct cell {
int id ; // mostly for parkour
int coord_x ;
int coord_y ;
bool star_allowed ;
CELLTYPE type ;
struct cell* prev_1 ;
struct cell* prev_2 ;
struct cell* prev_3 ;
struct cell* prev_4 ;
struct cell* prev_5 ;
struct cell* next_1 ;
struct cell* next_2 ;
struct cell* next_3 ;
struct cell* next_4 ;
struct cell* next_5 ; // I dont wanna deal with free/malloc there pls
char* meta ; // mostly for event spaces (also for hidden blocks)
} cell ;
typedef struct playerStats {
int minigames_wins ;
int coin_collected ;
int red_count ;
int bowser_count ;
int items_used ;
int lost_coins ;
int lost_stars ;
int tiles_travelled ;
int events_triggered ;
int hidden_blocks ;
} playerStats ;
typedef struct player {
char* name ;
cell* standing_tile ;
int coins ;
int stars ;
ITEM* items ;
int item_len ;
int max_items ;
playerStats stats ;
} player ;
typedef struct imgs {int len; SDL_Texture** arr;} imgs ;
static int* cell_count ;
static int* item_costs ;
static int* item_start ;
static int* item_end ;
static bool* item_is_normal_type ;
static bool* item_is_special_type ;
static int n_turns = 20;
static int n_star_spaces = 5 ;
static int star_cost = 20 ;
static cell* start ; // starting tile
static cell** playerCells ;
static int n_players ;
static player* players ;
static imgs* digits ;
// len is always 10 for this
#endif

72
src/display.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "consts.h"
#include "display.h"
void updateRenderer(SDL_Renderer* renderer) {
//printf("E");
SDL_RenderPresent(renderer);
}
void resetRenderer(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
void drawRectToRenderer(SDL_Renderer* renderer, SDL_Rect* rect, int R, int G, int B, int A) {
SDL_SetRenderDrawColor(renderer, R, G, B, A);
SDL_RenderFillRect(renderer, rect);
}
void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int R, int G, int B, int A) {
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_SetRenderDrawColor(renderer, R, G, B, A);
SDL_RenderFillRect(renderer, &rect);
}
void drawDigitToRenderer(SDL_Renderer* renderer, imgs data, int digit, int X, int Y, int W, int H) {
if (!(0 <= digit && digit <= 9))
{
fprintf(stderr, "Illegal digit : '%d'.\n", digit);
exit(1);
}
SDL_Rect rect;
rect.x = X;
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_Texture* texture = data.arr[digit];
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset) {
if(n == 0) {
drawDigitToRenderer(renderer, data, 0, X + W, Y, W, H);
} else {
int toDraw = 0, remaining = n, nIter = ln_baseN(n, 10);
while(nIter != 0) {
toDraw = remaining%10;
remaining = remaining / 10;
drawDigitToRenderer(renderer, data, toDraw, X + (W-Woffset)*nIter, Y, W, H);
nIter--;
}
}
}

16
src/display.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef GBM_DISPLAY_H
#define GBM_DISPLAY_H
void updateRenderer(SDL_Renderer* renderer);
void resetRenderer(SDL_Renderer* renderer);
void drawRectToRenderer(SDL_Renderer* renderer, SDL_Rect* rect, int R, int G, int B, int A);
void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int R, int G, int B, int A);
void drawDigitToRenderer(SDL_Renderer* renderer, imgs data, int digit, int X, int Y, int W, int H);
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset);
#endif

55
src/main.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "consts.h"
#include "display.h"
#include "base.h"
#include "assembly.h"
int main(int argc, char** argv) {
srand(time(NULL));
//-------------------------------------------------------------------------------//
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1200, 800, 0);
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
//-------------------------------------------------------------------------------//
if(SDL_Init(SDL_INIT_AUDIO)) {
fprintf(stderr, "cannot initialize audio");
exit(1);
}
/* -------------------------------------------------------- */
init_everything();
destroy_everything();
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}