Some refactoring.

This commit is contained in:
2023-11-10 12:56:21 -08:00
parent f6688acfc7
commit fe5cd908f8
5 changed files with 153 additions and 106 deletions

View File

@@ -1,114 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL_ttf.h>
#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)
{

37
queue.c Normal file
View File

@@ -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;
}

14
queue.h Normal file
View File

@@ -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

20
settings.h Normal file
View File

@@ -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

77
types.h Normal file
View File

@@ -0,0 +1,77 @@
#ifndef TYPES_H_SENTRY
#define TYPES_H_SENTRY
#include <stdlib.h>
#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