added primitive display + map editor

This commit is contained in:
Alexandre 2024-08-19 15:12:16 +02:00
parent 1b9077d38b
commit f6aeb5d573
16 changed files with 284 additions and 29 deletions

View File

@ -7,7 +7,7 @@ all: bin/mamaker
test: bin/mamaker
bin/mamaker
bin/mamaker: obj/main.o obj/base.o obj/assembly.o obj/display.o
bin/mamaker: obj/main.o obj/base.o obj/edit.o obj/assembly.o obj/display.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -18,6 +18,7 @@ obj/%.o: src/%.c
obj/main.o: src/main.c
obj/base.o: src/base.c
obj/assembly.o: src/assembly.c
obj/edit.o: src/edit.c
obj/display.o: src/display.c
.PHONY: clean mrproper

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/edit.o Normal file

Binary file not shown.

BIN
obj/main.o Normal file

Binary file not shown.

View File

@ -14,24 +14,44 @@
#include "consts.h"
#include "base.h"
#include "display.h"
#include "edit.h"
#include "assembly.h"
cell* start ;
int* item_costs ;
int* item_start ;
int* item_end ;
bool* item_is_normal_type ;
bool* item_is_special_type ;
int* cell_count ;
cell** playerCells ;
player* players ;
void init_empty_board() {
start = malloc(sizeof(cell));
start->id = 0 ;
start->coord_x = 50 ;
start->coord_y = 50 ;
start->coord_x = 0 ;
start->coord_y = 0 ;
start->star_allowed = false ;
start->type = START ;
start->prev_1 = start;
start->prev_1 = NULL;
start->prev_2 = NULL;
start->prev_3 = NULL;
start->prev_4 = NULL;
start->next_1 = start;
start->next_1 = NULL;
start->next_2 = NULL;
start->next_3 = NULL;
start->next_4 = NULL;
start->meta = "start";
cell_count = malloc(sizeof(int)*n_cell_type);
cell_count[0] = 1;
for(int i = 1; i < n_cell_type; i++) {
cell_count[i] = 0;
}
}
void init_players() {
@ -132,7 +152,7 @@ void init_everything() {
init_players();
printf("Initializing items...\n");
init_shop("src/config.txt");
printf("Initialization complete...\n");
printf("Initialization complete!\n\n");
//print_items();
}
@ -145,6 +165,7 @@ void print_items() {
}
void destroy_everything() {
printf("Destroying everything...\n");
for(int i = 0; i < n_players; i++) {
free(players[i].items);
};
@ -155,6 +176,8 @@ void destroy_everything() {
free(item_is_normal_type);
free(item_is_special_type);
free(cell_count);
printf("Destruction complete!...\n\n");
}
#include "consts.h"

View File

@ -14,6 +14,8 @@
#include "consts.h"
#include "base.h"
imgs digits ;
int ln_baseN(int n, int b) {
int r = 0;
while(n != 0) {
@ -33,19 +35,43 @@ int pw(int x, int n) {
return x*pw(x*x, n/2);
}
int abs(int n) {
if(n > 0) {
return n;
} else {
return (-n);
}
}
double absf(double n) {
if(n > 0.0f) {
return n;
} else {
return (-n);
}
}
int convex_seg(int x1, int x2, double theta) {
return (int)(((1.0f - theta) * x1 + theta * x2));
}
bool is_an_integer(char c) {
return ((int)c >= 48 && (int)c <= 57);
}
float to_float(int n) {
return (float)n ;
double to_double(int n) {
return (double)n ;
}
float to_int(float n) {
int to_int(double n) {
return (int)n ;
}
imgs import_digits(SDL_Renderer* renderer) {
double distance_pt(int x1, int x2, int y1, int y2) {
return sqrt(to_double(pw(x2 - x1, 2) + pw(y2 - y1, 2)));
}
void import_digits(SDL_Renderer* renderer) {
imgs res;
res.arr = malloc(sizeof(SDL_Texture*)*10);
SDL_Texture* texture;
@ -111,7 +137,7 @@ imgs import_digits(SDL_Renderer* renderer) {
SDL_FreeSurface(img);
res.arr[9] = texture;
return res;
digits = res;
}
void free_digits(imgs dgts) {
@ -119,4 +145,12 @@ void free_digits(imgs dgts) {
SDL_DestroyTexture(dgts.arr[i]);
}
free(dgts.arr);
}
bool is_star_allowed(int ctype) {
return ((CELLTYPE)ctype == BLUE) ;
}
bool is_hidden_block_allowed(int ctype) {
return ((CELLTYPE)ctype == BLUE || (CELLTYPE)ctype == RED) ;
}

View File

@ -5,14 +5,26 @@ int ln_baseN(int n, int b);
int pw(int x, int n);
int abs(int n);
double absf(double n);
int convex_seg(int x1, int x2, double theta);
bool is_an_integer(char c);
float to_float(int n);
double to_double(int n);
int to_int(float n);
int to_int(double n);
imgs import_digits(SDL_Renderer* renderer);
double distance_pt(int x1, int x2, int y1, int y2);
void import_digits(SDL_Renderer* renderer);
void free_digits(imgs dgts);
bool is_star_allowed(int ctype);
bool is_hidden_block_allowed(int ctype);
#endif

View File

@ -75,27 +75,29 @@ typedef struct player {
typedef struct imgs {int len; SDL_Texture** arr;} imgs ;
static int* cell_count ;
// arrays
extern 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 ;
extern int* item_costs ;
extern int* item_start ;
extern int* item_end ;
extern bool* item_is_normal_type ;
extern bool* item_is_special_type ;
// global
static int n_turns = 20;
static int n_star_spaces = 5 ;
static int star_cost = 20 ;
static cell* start ; // starting tile
extern cell* start ; // starting tile
static cell** playerCells ;
extern cell** playerCells ;
static int n_players ;
static player* players ;
extern player* players ;
static imgs* digits ;
extern imgs digits ;
// len is always 10 for this
static int __width__ = 1200 ;

View File

@ -40,6 +40,25 @@ void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int
SDL_RenderFillRect(renderer, &rect);
}
void drawLineWithThicc(SDL_Renderer* renderer, int width, int x1, int x2, int y1, int y2, int R, int G, int B, int A) {
double theta = 0.0;
double seglen = distance_pt(x1, x2, y1, y2);
while(theta < 1.0) {
placeRectToRenderer(renderer, convex_seg(x1, x2, theta)-width/2, convex_seg(y1, y2, theta)-width/2, width, width, R, G, B, A);
theta += 1 / seglen;
}
}
void drawLineWithThiccGradient(SDL_Renderer* renderer, int start_width, int end_width, int x1, int x2, int y1, int y2, int R, int G, int B, int A) {
double theta = 0.0;
double seglen = distance_pt(x1, x2, y1, y2);
while(theta < 1.0) {
int width = convex_seg(start_width, end_width, theta) ;
placeRectToRenderer(renderer, convex_seg(x1, x2, theta)-width/2, convex_seg(y1, y2, theta)-width/2, width, width, R, G, B, A);
theta += 1 / seglen;
}
}
void drawDigitToRenderer(SDL_Renderer* renderer, imgs data, int digit, int X, int Y, int W, int H) {
if (!(0 <= digit && digit <= 9))
{
@ -74,12 +93,25 @@ void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y
void draw_cell(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax, cell* cl, int draw_id) {
if(cl != NULL && cl->id != draw_id && cl->coord_x - tile_size > xmin && cl->coord_x - tile_size < xmax && cl->coord_y > ymin && cl->coord_y < ymax) {
int nxmin = to_int((to_float(cl->coord_x) - to_float(xmin)) * to_float(__width__)) / (to_float(xmax) - to_float(xmin)) ;
int nymin = to_int((to_float(cl->coord_y) - to_float(ymin)) * to_float(__height__)) / (to_float(ymax) - to_float(ymin)) ;
placeRectToRenderer(renderer, nxmin, nymin, tile_size, tile_size, 255, 255, 255, SDL_ALPHA_OPAQUE);
// draw lines
cl->id = draw_id;
int nxmin = to_int((to_double(cl->coord_x) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)) ;
int nymin = to_int((to_double(cl->coord_y) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)) ;
cl->id = draw_id ;
if(cl->next_1 != NULL) {
drawLineWithThiccGradient(renderer, tile_size, 3, nxmin, to_int((to_double(cl->next_1->coord_x) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)), nymin, to_int((to_double(cl->next_1->coord_y) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)), 255, 255, 0, SDL_ALPHA_OPAQUE);
};
if(cl->next_2 != NULL) {
drawLineWithThiccGradient(renderer, tile_size, 3, nxmin, to_int((to_double(cl->next_2->coord_x) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)), nymin, to_int((to_double(cl->next_2->coord_y) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)), 255, 255, 0, SDL_ALPHA_OPAQUE);
};
if(cl->next_3 != NULL) {
drawLineWithThiccGradient(renderer, tile_size, 3, nxmin, to_int((to_double(cl->next_3->coord_x) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)), nymin, to_int((to_double(cl->next_3->coord_y) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)), 255, 255, 0, SDL_ALPHA_OPAQUE);
};
if(cl->next_4 != NULL) {
drawLineWithThiccGradient(renderer, tile_size, 3, nxmin, to_int((to_double(cl->next_4->coord_x) - to_double(xmin)) * to_double(__width__)) / (to_double(xmax) - to_double(xmin)), nymin, to_int((to_double(cl->next_4->coord_y) - to_double(ymin)) * to_double(__height__)) / (to_double(ymax) - to_double(ymin)), 255, 255, 0, SDL_ALPHA_OPAQUE);
};
// recursive calls
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->prev_1, draw_id);
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->prev_2, draw_id);
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->prev_3, draw_id);
@ -89,11 +121,23 @@ void draw_cell(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax, c
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->next_2, draw_id);
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->next_3, draw_id);
draw_cell(renderer, xmin, xmax, ymin, ymax, cl->next_4, draw_id);
// draw current cell
placeRectToRenderer(renderer, nxmin-tile_size/2, nymin-tile_size/2, tile_size, tile_size, 255, 255, 255, SDL_ALPHA_OPAQUE);
} else if(cl != NULL) {
cl->id = draw_id;
}
}
void draw_board(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax, cell* one_cell) {
draw_cell(renderer, xmin, xmax, ymin, ymax, one_cell, one_cell->id +1);
if(one_cell == NULL) {
fprintf(stderr, "ERROR : cannot draw NULL cell\n");
exit(1);
} else {
resetRenderer(renderer);
draw_cell(renderer, xmin, xmax, ymin, ymax, one_cell, (one_cell->id +1)%727);
updateRenderer(renderer);
}
}

View File

@ -9,6 +9,10 @@ void drawRectToRenderer(SDL_Renderer* renderer, SDL_Rect* rect, int R, int G, in
void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int R, int G, int B, int A);
void drawLineWithThicc(SDL_Renderer* renderer, int width, int x1, int x2, int y1, int y2, int R, int G, int B, int A);
void drawLineWithThiccGradient(SDL_Renderer* renderer, int start_width, int end_width, int x1, int x2, int y1, int y2, 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);

111
src/edit.c Normal file
View File

@ -0,0 +1,111 @@
#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 "edit.h"
void link_cells(cell* parent, cell* child) {
if(parent != NULL) {
if(parent->next_1 == NULL) {
parent->next_1 = child;
} else if(parent->next_2 == NULL) {
parent->next_2 = child;
} else if(parent->next_3 == NULL) {
parent->next_3 = child;
} else if(parent->next_4 == NULL) {
parent->next_4 = child;
} else {
fprintf(stderr, "Unable to link cell : parent is at full capacity.\n");
}
};
if(child != NULL) {
if(child->prev_1 == NULL) {
child->prev_1 = parent;
} else if(child->prev_2 == NULL) {
child->prev_2 = parent;
} else if(child->prev_3 == NULL) {
child->prev_3 = parent;
} else if(child->prev_4 == NULL) {
child->prev_4 = parent;
} else {
fprintf(stderr, "Unable to link cell : child is at full capacity.\n");
}
}
}
void unlink_cells(cell* parent, cell* child) {
if(parent != NULL) {
if(parent->next_1 == child) {
parent->next_1 = NULL;
} else if(parent->next_2 == child) {
parent->next_2 = NULL;
} else if(parent->next_3 == child) {
parent->next_3 = NULL;
} else if(parent->next_4 == child) {
parent->next_4 = NULL;
} else {
fprintf(stderr, "Unable to unlink cell : no link found.\n");
}
};
if(child != NULL) {
if(child->prev_1 == parent) {
child->prev_1 = NULL;
} else if(child->prev_2 == parent) {
child->prev_2 = NULL;
} else if(child->prev_3 == parent) {
child->prev_3 = NULL;
} else if(child->prev_4 == parent) {
child->prev_4 = NULL;
} else {
fprintf(stderr, "Unable to unlink cell : no link found.\n");
}
};
}
void append_cell(int cell_type, int X, int Y, cell* parent) {
cell* new = malloc(sizeof(cell));
new->id = parent->id; // mostly for parkour
new->coord_x = X;
new->coord_y = Y;
new->star_allowed = is_star_allowed(cell_type);
new->type = (CELLTYPE)cell_type ;
new->prev_1 = NULL ;
new->prev_2 = NULL;
new->prev_3 = NULL;
new->prev_4 = NULL;
new->next_1 = NULL;
new->next_2 = NULL;
new->next_3 = NULL;
new->next_4 = NULL;
char* meta = "none";
link_cells(parent, new);
cell_count[cell_type] += 1;
}
void destroy_cell(cell* cl) {
unlink_cells(cl, cl->next_1);
unlink_cells(cl, cl->next_2);
unlink_cells(cl, cl->next_3);
unlink_cells(cl, cl->next_4);
unlink_cells(cl->prev_1, cl);
unlink_cells(cl->prev_2, cl);
unlink_cells(cl->prev_3, cl);
unlink_cells(cl->prev_4, cl);
cell_count[(int)cl->type] -= 1;
free(cl);
}

12
src/edit.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef GBM_EDIT_H
#define GBM_EDIT_H
void link_cells(cell* parent, cell* child);
void unlink_cells(cell* parent, cell* child);
void append_cell(int cell_type, int X, int Y, cell* parent);
void destroy_cell(cell* cl);
#endif

View File

@ -14,6 +14,7 @@
#include "consts.h"
#include "display.h"
#include "base.h"
#include "edit.h"
#include "assembly.h"
int main(int argc, char** argv) {
@ -42,6 +43,17 @@ int main(int argc, char** argv) {
/* -------------------------------------------------------- */
init_everything();
draw_board(rend, -250, 250, -250, 250, start);
sleep(1);
append_cell(1, 75, 75, start);
draw_board(rend, -250, 250, -250, 250, start);
sleep(1);
append_cell(1, 75, 0, start->next_1);
draw_board(rend, -250, 250, -250, 250, start);
sleep(1);
destroy_everything();