Some refactoring.
This commit is contained in:
111
pingpong.c
111
pingpong.c
@@ -1,114 +1,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL_ttf.h>
|
#include <SDL_ttf.h>
|
||||||
|
|
||||||
#define TRUE 1
|
#include "types.h"
|
||||||
#define FALSE 0
|
#include "settings.h"
|
||||||
|
#include "queue.h"
|
||||||
#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;
|
|
||||||
|
|
||||||
void exit_error(const char *error)
|
void exit_error(const char *error)
|
||||||
{
|
{
|
||||||
|
|||||||
37
queue.c
Normal file
37
queue.c
Normal 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
14
queue.h
Normal 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
20
settings.h
Normal 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
77
types.h
Normal 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
|
||||||
Reference in New Issue
Block a user