From fe5cd908f8e499e18a3c02dfd1ba5684b5c37fd5 Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 10 Nov 2023 12:56:21 -0800 Subject: [PATCH] Some refactoring. --- pingpong.c | 111 +++-------------------------------------------------- queue.c | 37 ++++++++++++++++++ queue.h | 14 +++++++ settings.h | 20 ++++++++++ types.h | 77 +++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 106 deletions(-) create mode 100644 queue.c create mode 100644 queue.h create mode 100644 settings.h create mode 100644 types.h diff --git a/pingpong.c b/pingpong.c index 5273b25..501528f 100644 --- a/pingpong.c +++ b/pingpong.c @@ -1,114 +1,13 @@ #include +#include #include + #include #include -#define TRUE 1 -#define FALSE 0 - -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 - -#define FPS 120 -#define TARGET_FRAME_TIME (1000 / FPS) - -typedef struct { - float x; - float y; -} vector2; - -typedef struct { - float r; - float g; - float b; -} vector3; - -typedef struct { - float r; - float g; - float b; - float a; -} vector4; - -typedef struct { - float w; - float h; -} rect; - -typedef struct { - vector2 point; - vector2 velocity; - vector2 dimension; - vector4 color; -} particle; - -struct node { - struct node *next; - particle data; -}; - -typedef struct { - struct node *first; - struct node *last; -} queue; - -void qinit(queue *item) -{ - item->first = NULL; - item->last = NULL; -} - -void qput(queue *item, particle data) -{ - if(!item->first) { - item->first = malloc(sizeof(struct node)); - item->last = item->first; - } else { - item->last->next = malloc(sizeof(struct node)); - item->last = item->last->next; - } - item->last->data = data; - item->last->next = NULL; -} - -void qget(queue *item, particle *data) -{ - if(data) - *data = item->first->data; - - struct node *tmp = item->first; - item->first = item->first->next; - if(!item->first) - item->last = NULL; - free(tmp); -} - -int qempty(queue *item) -{ - return !item->first; -} - -typedef struct { - int is_running; - int last_frame_time; - - struct ball { - vector2 point; - vector2 velocity; - vector2 dimension; - } ball; - - struct player { - vector2 point; - vector2 dimension; - float vy; - int score; - int up; - int down; - } player_one, player_two; - - queue particles; -} game_state; +#include "types.h" +#include "settings.h" +#include "queue.h" void exit_error(const char *error) { diff --git a/queue.c b/queue.c new file mode 100644 index 0000000..0102917 --- /dev/null +++ b/queue.c @@ -0,0 +1,37 @@ +#include "queue.h" + +void qinit(queue *item) +{ + item->first = NULL; + item->last = NULL; +} + +void qput(queue *item, particle data) +{ + if(!item->first) { + item->first = malloc(sizeof(struct node)); + item->last = item->first; + } else { + item->last->next = malloc(sizeof(struct node)); + item->last = item->last->next; + } + item->last->data = data; + item->last->next = NULL; +} + +void qget(queue *item, particle *data) +{ + if(data) + *data = item->first->data; + + struct node *tmp = item->first; + item->first = item->first->next; + if(!item->first) + item->last = NULL; + free(tmp); +} + +int qempty(queue *item) +{ + return !item->first; +} diff --git a/queue.h b/queue.h new file mode 100644 index 0000000..bc553b6 --- /dev/null +++ b/queue.h @@ -0,0 +1,14 @@ +#ifndef QUEUE_H_SENTRY +#define QUEUE_H_SENTRY + +#include "types.h" + +void qinit(queue *item); + +void qput(queue *item, particle data); + +void qget(queue *item, particle *data); + +int qempty(queue *item); + +#endif diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..f1a7c0a --- /dev/null +++ b/settings.h @@ -0,0 +1,20 @@ +#ifndef SETTINGS_H_SENTRY +#define SETTINGS_H_SENTRY + +#ifndef WINDOW_WIDTH +#define WINDOW_WIDTH 800 +#endif + +#ifndef WINDOW_HEIGHT +#define WINDOW_HEIGHT 600 +#endif + +#ifndef FPS +#define FPS 120 +#endif + +#ifndef TARGET_FRAME_TIME +#define TARGET_FRAME_TIME (1000 / FPS) +#endif + +#endif diff --git a/types.h b/types.h new file mode 100644 index 0000000..7e847ce --- /dev/null +++ b/types.h @@ -0,0 +1,77 @@ +#ifndef TYPES_H_SENTRY +#define TYPES_H_SENTRY + +#include + + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +typedef struct { + float x; + float y; +} vector2; + +typedef struct { + float r; + float g; + float b; +} vector3; + +typedef struct { + float r; + float g; + float b; + float a; +} vector4; + +typedef struct { + float w; + float h; +} rect; + +typedef struct { + vector2 point; + vector2 velocity; + vector2 dimension; + vector4 color; +} particle; + +struct node { + struct node *next; + particle data; +}; + +typedef struct { + struct node *first; + struct node *last; +} queue; + +typedef struct { + int is_running; + int last_frame_time; + + struct ball { + vector2 point; + vector2 velocity; + vector2 dimension; + } ball; + + struct player { + vector2 point; + vector2 dimension; + float vy; + int score; + int up; + int down; + } player_one, player_two; + + queue particles; +} game_state; + +#endif