added being able to create a host on the game server now need to work on joining that game

This commit is contained in:
2026-02-06 16:36:13 -06:00
parent 468ab94aa7
commit b2fa3052f3
23 changed files with 432 additions and 326 deletions

View File

@@ -19,7 +19,7 @@ ifeq ($(debug),on)
CPPFLAGS += -Ddebug CPPFLAGS += -Ddebug
endif endif
BINARIES = game client server BINARIES = game game_server
all : $(BINARIES) all : $(BINARIES)
@@ -66,10 +66,7 @@ $(OBJ)overworld.o : overworld.c $(INC)overworld.h | $(OBJ)
$(OBJ)net.o : net.c $(INC)net.h | $(OBJ) $(OBJ)net.o : net.c $(INC)net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS) $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)client.o : client.c $(INC)net.h | $(OBJ) $(OBJ)game_server.o : game_server.c $(INC)net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)server.o : server.c $(INC)net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS) $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
@@ -84,11 +81,8 @@ $(OBJ)server.o : server.c $(INC)net.h | $(OBJ)
#$(BIN) : $(OBJ) | $(BIN) #$(BIN) : $(OBJ) | $(BIN)
# $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) # $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
$(BIN)client : $(OBJ)client.o | $(BIN) $(BIN)game_server: $(OBJ)game_server.o $(OBJ)net.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(BIN)server: $(OBJ)server.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
$(BIN)game : $(OBJ)main.o $(OBJ)input.o $(OBJ)battle.o $(OBJ)game.o\ $(BIN)game : $(OBJ)main.o $(OBJ)input.o $(OBJ)battle.o $(OBJ)game.o\
$(OBJ)options.o $(OBJ)host.o $(OBJ)join.o $(OBJ)overworld.o $(OBJ)net.o\ $(OBJ)options.o $(OBJ)host.o $(OBJ)join.o $(OBJ)overworld.o $(OBJ)net.o\

1
client
View File

@@ -1 +0,0 @@
build/bin/client

View File

@@ -19,7 +19,6 @@ int main(void)
IPaddress ip; IPaddress ip;
TCPsocket sock; TCPsocket sock;
int ret = 0;
SDL_Init(0); SDL_Init(0);

1
game_server Symbolic link
View File

@@ -0,0 +1 @@
build/bin/game_server

View File

@@ -6,23 +6,32 @@
#include <stdio.h> #include <stdio.h>
#include <mydebug.h> #include <mydebug.h>
#include <net.h> #include <net.h>
#include <game.h>
#define MAX_SOCKS 4
char buff[PACKSZ]; #define MAX_LOBBIES 16
char pack[PACKSZ], pack2[PACKSZ];
host_info open_hosts[MAX_LOBBIES];
SDLNet_SocketSet host_socks;
extern char my_buff[PACKSZ];
extern char net_buff[PACKSZ];
packet ser_send, recved;
void new_host_setup();
TCPsocket sock, csock;
void game_server() void game_server()
{ {
IPaddress *ip, _ip, *cip; IPaddress *ip, _ip, *cip;
TCPsocket sock, csock; TCPsocket sock_arr[MAX_LOBBIES];
TCPsocket sock_arr[MAX_SOCKS];
ip = &_ip; ip = &_ip;
SDLNet_SocketSet socks; SDLNet_SocketSet socks;
int i; int i;
Uint16 c_port;
Uint32 c_ip;
int flag = 1; int flag = 1;
int ret = 0; int ret = 0;
@@ -44,24 +53,26 @@ void game_server()
exit(1); exit(1);
} }
socks = SDLNet_AllocSocketSet(MAX_SOCKS); socks = SDLNet_AllocSocketSet(MAX_LOBBIES);
for(i = 0; i < MAX_SOCKS; ++i) for(i = 0; i < MAX_LOBBIES; ++i)
{ {
open_hosts[i].name[0] = '\0';
sock_arr[i] = NULL; sock_arr[i] = NULL;
} }
//main server loop
while(flag) while(flag)
{ {
csock = SDLNet_TCP_Accept(sock); csock = SDLNet_TCP_Accept(sock);
if(!csock && SDLNet_CheckSockets(socks, 0) > 0 ){ if(!csock && SDLNet_CheckSockets(socks, 0) > 0 ){
//no new connection and there is updates on socks //no new connection and there is updates on socks
for(i = 0; i < MAX_SOCKS; ++i) for(i = 0; i < MAX_LOBBIES; ++i)
{//check each sock for data and handle it {//check each sock for data and handle it
csock = sock_arr[i]; csock = sock_arr[i];
if(csock == NULL){continue;} if(csock == NULL){continue;}
if(SDLNet_SocketReady(csock)){ if(SDLNet_SocketReady(csock)){
ret = SDLNet_TCP_Recv(csock, pack, PACKSZ); ret = SDLNet_TCP_Recv(csock, net_buff, PACKSZ);
if(ret < PACKSZ){ if(ret < PACKSZ){
printf("TCP recv error: %s\n", SDLNet_GetError()); printf("TCP recv error: %s\n", SDLNet_GetError());
sock_arr[i] = NULL; sock_arr[i] = NULL;
@@ -69,8 +80,8 @@ void game_server()
SDLNet_TCP_Close(csock); SDLNet_TCP_Close(csock);
continue; continue;
} }
read_to_buff((Uint32*)buff, (Uint32*)pack, PACKSZ); read_from_packet((Uint32*)&recved, (Uint32*)net_buff, PACKSZ);
switch(((packet*)buff)->type) switch(recved.type)
{ {
case G_STATE: case G_STATE:
PRINT_M("got to get state case"); PRINT_M("got to get state case");
@@ -83,13 +94,16 @@ void game_server()
break; break;
case NEW_HOST: case NEW_HOST:
PRINT_M("got to new host state"); PRINT_M("got to new host state");
new_host_setup();
break; break;
case JOIN_REQ: case JOIN_REQ:
PRINT_M("got to join request state"); PRINT_M("got to join request state");
break; break;
case U_NAME: case U_NAME:
PRINT_M("got to update name state"); PRINT_M("got to update name state");
flag = 0; break;
default:
printf("unknown packet type: %d\n", recved.type);
break; break;
} }
} }
@@ -105,28 +119,43 @@ void game_server()
if(SDLNet_TCP_AddSocket(socks, csock) <= 0){ if(SDLNet_TCP_AddSocket(socks, csock) <= 0){
printf("add sock to set error: %s\n", SDLNet_GetError()); printf("add sock to set error: %s\n", SDLNet_GetError());
} }
for(i = 0; i < MAX_SOCKS; ++i){ for(i = 0; i < MAX_LOBBIES; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; } if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
} }
} }
} }
return;
} }
SDLNet_TCP_Close(sock); SDLNet_TCP_Close(sock);
SDLNet_Quit(); SDLNet_Quit();
SDL_Quit(); SDL_Quit();
} }
int main(void) int main(void)
{ {
game_server(); game_server();
puts("donzo");
return 0; return 0;
} }
void new_host_setup()
{
int i;
TCPsocket s = csock;
PRINT_M(SETUP NEW HOST)
for(i = 0; i < MAX_LOBBIES; ++i){
if(!*(open_hosts[i].name)){continue;}
if(!strcmp(recved.payload, open_hosts[i].name)){
PRINT_M(ALREAD HAVE THAT LOBBY NAME OPEN)
return;
}
}
PRINT_M(SENDING BACK THEY GOOD)
//no lobbies have that name
write_for_sending((Uint32*)net_buff, (Uint32*)&recved, PACKSZ);
SDLNet_TCP_Send(s, net_buff, PACKSZ);
return;
}

3
host.c
View File

@@ -118,7 +118,7 @@ char *get_this_name(int i)
{ {
if(!lobby_has_name) { if(!lobby_has_name) {
if(i == -1){return host.lobby_name; } if(i == -1){ return host.lobby_name; }
if(i == 0) { return host.lobby_name_perm; } if(i == 0) { return host.lobby_name_perm; }
} }
else{ else{
@@ -217,6 +217,7 @@ void render_main()
else if(SDL_PointInRect(&mouse_pos, &host.set_name_rect)){ else if(SDL_PointInRect(&mouse_pos, &host.set_name_rect)){
over_object = SET_NAME_BUTTON; over_object = SET_NAME_BUTTON;
} }
else {over_object = NO_OBJECT; }
if(lobby_has_name){ render_lobby(); } if(lobby_has_name){ render_lobby(); }
else{ render_lobby_name(); } else{ render_lobby_name(); }

View File

@@ -1,53 +0,0 @@
/*
CHAT: A chat client/server using the SDL example network library
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* $Id$ */
/* Convert four letters into a number */
#define MAKE_NUM(A, B, C, D) (((A+B)<<8)|(C+D))
/* Defines for the chat client */
#define CHAT_SCROLLBACK 512 /* Save 512 lines in scrollback */
#define CHAT_PROMPT "> "
#define CHAT_PACKETSIZE 256 /* Maximum length of a message */
/* Defines shared between the server and client */
#define CHAT_PORT MAKE_NUM('C','H','A','T')
/* The protocol between the chat client and server */
#define CHAT_HELLO 0 /* 0+Port+len+name */
#define CHAT_HELLO_PORT 1
#define CHAT_HELLO_NLEN CHAT_HELLO_PORT+2
#define CHAT_HELLO_NAME CHAT_HELLO_NLEN+1
#define CHAT_ADD 1 /* 1+N+IP+Port+len+name */
#define CHAT_ADD_SLOT 1
#define CHAT_ADD_HOST CHAT_ADD_SLOT+1
#define CHAT_ADD_PORT CHAT_ADD_HOST+4
#define CHAT_ADD_NLEN CHAT_ADD_PORT+2
#define CHAT_ADD_NAME CHAT_ADD_NLEN+1
#define CHAT_DEL 2 /* 2+N */
#define CHAT_DEL_SLOT 1
#define CHAT_DEL_LEN CHAT_DEL_SLOT+1
#define CHAT_BYE 255 /* 255 */
#define CHAT_BYE_LEN 1
/* The maximum number of people who can talk at once */
#define CHAT_MAXPEOPLE 10

View File

@@ -20,7 +20,6 @@ extern SDL_Point mouse_pos;
extern int state; extern int state;
void start_battle(); void start_battle();
void show_options();
#define GRAB_NEW 2 #define GRAB_NEW 2
#define GRAB_OLD 1 #define GRAB_OLD 1

View File

@@ -2,12 +2,17 @@
#ifndef __my_net_h__ #ifndef __my_net_h__
#define __my_net_h__ #define __my_net_h__
#include <SDL_net.h>
#include <game.h>
#define PORT 36911 #define PORT 36911
#define SERVER "localhost" #define SERVER "localhost"
#define PACKSZ 1024 #define PACKSZ 1024
enum pack_type{ enum pack_type{
G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME
}; };
@@ -17,32 +22,18 @@ typedef struct packet{
char payload[PACKSZ - sizeof(enum pack_type)]; char payload[PACKSZ - sizeof(enum pack_type)];
} packet; } packet;
typedef struct host_info
int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{ {
if(sz%32){ char name[MAX_NAMESZ];
puts("error size must be div by 32"); IPaddress ip;
return 0; }host_info;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
int write_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{ int read_from_packet(Uint32 *buff, Uint32 *src, Uint32 sz);
if(sz%32){ int write_for_sending(Uint32 *buff, Uint32 *src, Uint32 sz);
puts("error size must be div by 32");
return 0;
} int new_host(char l[MAX_NAMESZ]);
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*(src + i), (buff + i));
}
return 1;
}
#endif #endif

22
include/options.h Normal file
View File

@@ -0,0 +1,22 @@
//Troy Rosin
#ifndef __options_h__
#define __options_h__
//gameplay, video, audio, mainmenu, exit game
#define NUM_OPTIONS 5
#define GP_OPT_SEL 0
#define VID_OPT_SEL 1
#define AUD_OPT_SEL 2
#define MM_OPT_SEL 3
#define EXT_OPT_SEL 4
void init_options();
void render_options();
int get_sel_opt();
void set_sel_opt(int);
#endif

132
input.c
View File

@@ -6,36 +6,21 @@
#include <overworld.h> #include <overworld.h>
#include <host.h> #include <host.h>
#include <mydebug.h> #include <mydebug.h>
#include <options.h>
#include <net.h>
int prev_state;
void mainmenu_selection_state();
void handle_input_mainmenu(SDL_Event *e); void handle_input_mainmenu(SDL_Event *e);
void handle_input_battle(SDL_Event *e); void handle_input_battle(SDL_Event *e);
void handle_input_overworld(SDL_Event *e); void handle_input_overworld(SDL_Event *e);
void handle_input_host(SDL_Event *e); void handle_input_host(SDL_Event *e);
void handle_input_join(SDL_Event *e); void handle_input_join(SDL_Event *e);
void handle_input_options(SDL_Event *e);
void mainmenu_selection_state() //main event handler
{
switch (selected_index)
{
case START_GAME:
state = START;
break;
case HOST_GAME:
state = HOST;
break;
case JOIN_GAME:
state = JOIN;
break;
case OPTIONS_MENU:
state = OPTIONS;
break;
case EXIT_GAME:
running = 0;
break;
}
}
void handle_event(SDL_Event *e){ void handle_event(SDL_Event *e){
while(SDL_PollEvent(e)) while(SDL_PollEvent(e))
{ {
@@ -78,6 +63,9 @@ void handle_event(SDL_Event *e){
case JOIN: case JOIN:
handle_input_join(e); handle_input_join(e);
break; break;
case OPTIONS:
handle_input_options(e);
break;
default: default:
if(e->type == SDL_KEYDOWN && e->key.keysym.sym == SDLK_ESCAPE) if(e->type == SDL_KEYDOWN && e->key.keysym.sym == SDLK_ESCAPE)
{ {
@@ -89,12 +77,32 @@ void handle_event(SDL_Event *e){
} }
} }
void mainmenu_selection_state()
{
switch (selected_index)
{
case START_GAME:
state = START;
break;
case HOST_GAME:
state = HOST;
break;
case JOIN_GAME:
state = JOIN;
break;
case OPTIONS_MENU:
prev_state = state;
state = OPTIONS;
break;
case EXIT_GAME:
running = 0;
break;
}
}
void handle_input_mainmenu(SDL_Event *e) void handle_input_mainmenu(SDL_Event *e)
{ {
switch(e->key.keysym.sym){ switch(e->key.keysym.sym){
case SDLK_ESCAPE:
running = 0;
break;
case SDLK_UP: case SDLK_UP:
case SDLK_w: case SDLK_w:
if (selected_index) { selected_index--; } if (selected_index) { selected_index--; }
@@ -131,7 +139,8 @@ void handle_input_overworld(SDL_Event *e)
switch(e->key.keysym.sym) switch(e->key.keysym.sym)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE:
running = 0; prev_state = state;
state = OPTIONS;
break; break;
case SDLK_UP: case SDLK_UP:
case SDLK_w: case SDLK_w:
@@ -170,7 +179,8 @@ void handle_input_host(SDL_Event *e)
switch(e->key.keysym.sym) switch(e->key.keysym.sym)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE:
running = 0; PRINT_M(TELL SERVER IM NOT A HOST)
state = TITLE;
break; break;
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
PRINT_M(got backspace) PRINT_M(got backspace)
@@ -196,9 +206,18 @@ void handle_input_host(SDL_Event *e)
strcpy(get_this_name(0), nameptr); strcpy(get_this_name(0), nameptr);
break; break;
case START_GAME_BUTTON: case START_GAME_BUTTON:
if(strlen(get_this_name(0)) >= 1){ PRINT_M(CHECK THAT NAME IS VALID!!)
if(strlen(get_this_name(0)) >= 1 && !lobby_has_name){
if(new_host(get_this_name(0))){
lobby_has_name = 1; lobby_has_name = 1;
} }
else{
PRINT_M(ERROR GOT NEG FOR NEW HOST)
}
}
else{//were in the lobby waiting for homies
PRINT_M(START COOP GAME)
}
break; break;
} }
} }
@@ -221,7 +240,7 @@ void handle_input_join(SDL_Event *e)
switch(e->key.keysym.sym) switch(e->key.keysym.sym)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE:
running = 0; state = TITLE;
break; break;
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
PRINT_M(got backspace) PRINT_M(got backspace)
@@ -259,3 +278,58 @@ void handle_input_join(SDL_Event *e)
} }
return; return;
} }
void options_select(int sel)
{
switch(sel)
{
case GP_OPT_SEL:
PRINT_M(GAMEPLAY OPTIONS)
break;
case VID_OPT_SEL:
PRINT_M(VIDEO OPTIONS)
break;
case AUD_OPT_SEL:
PRINT_M(AUDIO OPTIONS)
break;
case MM_OPT_SEL:
PRINT_M(HANDLE EXITING GRACEFULLY I.E. SAVING GAME)
prev_state = state = TITLE;
break;
case EXT_OPT_SEL:
PRINT_M(EXIT GAME)
running = 0;
break;
default:
PRINT_M(UNKNOWN OPTION PASSED)
break;
}
return;
}
void handle_input_options(SDL_Event *e)
{
int sel = get_sel_opt();
if(e->type == SDL_KEYUP){ return; }
switch(e->key.keysym.sym){
case SDLK_ESCAPE:
PRINT_M(NEED TO GET PREV STATE)
state = prev_state;
break;
case SDLK_UP:
case SDLK_w:
if (sel) { sel--; }
else { sel = NUM_OPTIONS - 1; }
break;
case SDLK_DOWN:
case SDLK_s:
++sel;
sel %= NUM_OPTIONS;
break;
case SDLK_RETURN:
options_select(sel);
break;
}
set_sel_opt(sel);
return;
}

1
join.c
View File

@@ -102,6 +102,7 @@ void init_join()
PRINT_M(INIT JOIN) PRINT_M(INIT JOIN)
return;
} }
void join_update_hover() void join_update_hover()

4
main.c
View File

@@ -5,6 +5,7 @@
#include <input.h> #include <input.h>
#include <overworld.h> #include <overworld.h>
#include <host.h> #include <host.h>
#include <options.h>
#include <mydebug.h> #include <mydebug.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@@ -47,6 +48,7 @@ int main(void)
init_join(); init_join();
init_host(); init_host();
init_overworld(); init_overworld();
init_options();
running = 1; running = 1;
@@ -72,7 +74,7 @@ int main(void)
render_join_game(); render_join_game();
break; break;
case OPTIONS: case OPTIONS:
show_options(); render_options();
break; break;
case TITLE: case TITLE:
render_mainmenu(); render_mainmenu();

97
net.c
View File

@@ -2,7 +2,6 @@
#include <SDL_net.h> #include <SDL_net.h>
#include <SDLnetsys.h> #include <SDLnetsys.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <mydebug.h> #include <mydebug.h>
@@ -12,15 +11,95 @@
#include <main.h> #include <main.h>
#include <net.h> #include <net.h>
IPaddress ser_ip;
TCPsocket ser_sock;
SDLNet_SocketSet sset;
char my_buff[PACKSZ]; //always client bit ordering
char net_buff[PACKSZ]; //always in net bit ordering
packet to_send, received;
int init_server_connection()
{
static int connected = 0;
if(connected){ return 1; }
SDLNet_Init();
if(SDLNet_ResolveHost(&ser_ip, SERVER, PORT) == -1){
printf("SDLNet resolve host, in init_server connection, error,\n%s\n",
SDLNet_GetError());
return 0;
}
ser_sock = SDLNet_TCP_Open(&ser_ip);
if(!ser_sock){
printf("SDLNet tcp open, in init_server connection, error,\n%s\n",
SDLNet_GetError());
return 0;
}
sset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(sset, ser_sock);
connected = 1;
return 1;
}
static TCPsocket tcpscok = NULL;
static UDPsocket udpsock = NULL; int new_host(char lobby_name[MAX_NAMESZ])
static SDLNet_SocketSet socketset = NULL; {
static UDPpacket **packets = NULL; int ret;
static struct { if(!init_server_connection()){
int active; return 0;
Uint8 name[MAX_NAMESZ]; }
} people[MAX_PLAYERS]; strcpy(to_send.payload, lobby_name);
to_send.type = NEW_HOST;
write_for_sending((Uint32*)net_buff, (Uint32*)&to_send, PACKSZ);
SDLNet_TCP_Send(ser_sock, net_buff, PACKSZ);
//wait for a response
while(!SDLNet_CheckSockets(sset, 0)){}
PRINT_M(HAVE DATA TO GET)
ret = SDLNet_TCP_Recv(ser_sock, net_buff, PACKSZ);
if(ret < PACKSZ){ return 0; }
PRINT_M(GOT FULL PACK)
read_from_packet((Uint32*)&received, (Uint32*)net_buff, PACKSZ);
if(received.type != NEW_HOST){ return 0; }
PRINT_M(NEW_HOST CREATED!)
return 1;
}
//takes packet in host bit ordering and puts in buff with client bit ordering
int read_from_packet(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
//takes buffer in client bit order and packs into packet with net bit ordering
int write_for_sending(Uint32 *pack, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*(src + i), (pack + i));
}
return 1;
}

View File

@@ -1,36 +0,0 @@
#Troy Rosin tjr760 11200680
CC =gcc
CFLAGS =-g
CPPFLAGS =-c -g -Wall -pedantic
LDFLAGS =-lSDL2 -lSDL2_net
BIN =build/bin/
LIB =build/lib/
OBJ =build/obj/
DIRS =$(BIN) $(LIB) $(OBJ)
debug ?=on
ifeq ($(debug),on)
CPPFLAGS += -Ddebug
endif
BINARIES = client server
all : $(BINARIES)
clean :
rm -rf build/ lib/ bin/ $(BINARIES)
$(DIRS) :
mkdir -p $@
$(BIN)% : $(OBJ)%.o | $(BIN)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(OBJ)%.o : %.c net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< -I.
$(BINARIES) : % : $(BIN)%
ln -sf $< $@

View File

@@ -1 +0,0 @@
build/bin/client

View File

@@ -1,84 +0,0 @@
/*for debugging print statments and timers
* Troy Rosin TJR760 11200680
* compile with -Ddebug and the prints will be there
* otherwise nothing wil appear
*
* to use with makefile add the following
*
* debug ?=off
* ifeq ($(debug),on)
* CPPFLAGS += -Ddebug
* endif
*
* then use "make debug=on" to make if you want the debug messages
* just use make to build normally
*
*/
#include <stdio.h>
#include <time.h>
#ifdef debug
/*prints the name of the function that was called passed as an arg */
#define PRINT_C(FUNC)\
puts(#FUNC" was called");
/*prints the message wanted followed by a \n no "" needed*/
#define PRINT_M(MSG)\
puts(#MSG);
/*prints the name of a variable holding a pointer and its value*/
#define PRINT_P(PTR)\
printf(#PTR" has value %p\n", (void*)PTR);
/*prints the varilble name holding the int and the value
* Type is how you want it to print ie l, d, ld, u, f...
* just pass the field type printf expects without the '%'
*/
#define PRINT_INT(VAR, TYPE)\
printf(#VAR" has value %"#TYPE"\n", VAR);
/*starts a timer so you can see how long fuctions can run for
* you must end the timer within the same scope as the start
* the id can be anything its only used to differentiate between
* timers
* T_START must be placed at the end of variable definitions to comply with C90
* standards. if not using C90 go nuts
*/
#define T_START(ID)\
struct timespec ID ## __d_timer_s__;\
struct timespec ID ## __d_timer_e__;\
clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_s__);
/*checks how long since the timer began and prints the seconds and nanoseconds
* since. Can be called multiple times if needed
*/
#define T_END(ID)\
clock_gettime(CLOCK_MONOTONIC_RAW, &ID ## __d_timer_e__);\
printf("timer "#ID" took:\n%ld second(s) total\n%ld nanoseconds total\n",\
((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec - \
ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) - \
((ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec-\
ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec) \
% 1000000000)) / 1000000000 , \
ID ## __d_timer_e__.tv_sec * 1000000000 + ID ## __d_timer_e__.tv_nsec -\
ID ## __d_timer_s__.tv_sec * 1000000000 + ID ## __d_timer_s__.tv_nsec );
#else
/*if debug was not defined we insert nothing there*/
#define PRINT_C(X) {}
#define PRINT_M(X) {}
#define PRINT_P(X) {}
#define PRINT_INT(X,Y) {}
#define T_START(X) {}
#define T_END(X) {}
#endif

View File

@@ -1,48 +0,0 @@
//Troy Rosin
#ifndef __my_net_h__
#define __my_net_h__
#define PORT 36911
#define SERVER "localhost"
#define PACKSZ 1024
enum pack_type{
G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME
};
typedef struct packet{
enum pack_type type;
char payload[PACKSZ - sizeof(enum pack_type)];
} packet;
int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
int write_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*(src + i), (buff + i));
}
return 1;
}
#endif

View File

@@ -1 +0,0 @@
build/bin/server

View File

@@ -1 +0,0 @@
www.libsdl.org/projects/old/SDL_net/docs/

114
options.c
View File

@@ -2,14 +2,124 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <mydebug.h> #include <mydebug.h>
#include <game.h> #include <game.h>
#include <input.h> #include <input.h>
#include <deck.h> #include <deck.h>
#include <main.h> #include <main.h>
#include <options.h>
void show_options(){ #define SPACING 20
running = 0;
struct options_ctx
{
int selected_opt;
char option_strings[NUM_OPTIONS][MAX_NAMESZ];
TTF_Font* font;
SDL_Surface *gameplay_surface, *video_surface, *audio_surface,
*mainmenu_surface, *exit_surface;
SDL_Texture *gameplay_tex, *video_tex, *audio_tex, *mainmenu_tex, *exit_tex;
SDL_Rect options_rects[NUM_OPTIONS];
SDL_Rect options_boxs[NUM_OPTIONS];
SDL_Texture *string_texs[NUM_OPTIONS];
};
struct options_ctx opt;
int get_sel_opt()
{
return opt.selected_opt;
}
void set_sel_opt(int i)
{
opt.selected_opt = i;
return;
}
void init_options()
{
int i;
opt.selected_opt = GP_OPT_SEL;
strcpy(opt.option_strings[0], "GAMEPLAY");
strcpy(opt.option_strings[1], "VIDEO");
strcpy(opt.option_strings[2], "AUDIO");
strcpy(opt.option_strings[3], "MAIN MENU");
strcpy(opt.option_strings[4], "EXIT GAME");
opt.font = TTF_OpenFont("assets/font.ttf", 24);
opt.gameplay_surface = TTF_RenderText_Solid(opt.font, opt.option_strings[0], white);
opt.gameplay_tex = SDL_CreateTextureFromSurface(renderer, opt.gameplay_surface);
opt.video_surface = TTF_RenderText_Solid(opt.font, opt.option_strings[1], white);
opt.video_tex = SDL_CreateTextureFromSurface(renderer, opt.video_surface);
opt.audio_surface = TTF_RenderText_Solid(opt.font, opt.option_strings[2], white);
opt.audio_tex = SDL_CreateTextureFromSurface(renderer, opt.audio_surface);
opt.mainmenu_surface = TTF_RenderText_Solid(opt.font, opt.option_strings[3], white);
opt.mainmenu_tex = SDL_CreateTextureFromSurface(renderer, opt.mainmenu_surface);
opt.exit_surface = TTF_RenderText_Solid(opt.font, opt.option_strings[4], white);
opt.exit_tex = SDL_CreateTextureFromSurface(renderer, opt.exit_surface);
opt.string_texs[0] = opt.gameplay_tex;
opt.string_texs[1] = opt.video_tex;
opt.string_texs[2] = opt.audio_tex;
opt.string_texs[3] = opt.mainmenu_tex;
opt.string_texs[4] = opt.exit_tex;
opt.options_boxs[0].w = (DEF_WINDOW_WIDTH >> 2) + (DEF_WINDOW_WIDTH >> 3) ;
opt.options_boxs[0].x = (DEF_WINDOW_WIDTH >> 1) - (opt.options_boxs[0].w >> 1);
opt.options_boxs[0].y = SPACING << 3;
opt.options_boxs[0].h = (DEF_WINDOW_HEIGHT >> 3);
opt.options_rects[0].w = ((opt.options_boxs[0].w - (SPACING << 1)) / 9) * strlen(opt.option_strings[0]);
opt.options_rects[0].h = opt.options_boxs[0].h - (SPACING << 1);
opt.options_rects[0].y = opt.options_boxs[0].y + SPACING;
opt.options_rects[0].x = opt.options_boxs[0].x + SPACING;
for(i = 1; i < NUM_OPTIONS; ++i)
{
opt.options_boxs[i].w = opt.options_boxs[0].w;
opt.options_boxs[i].x = opt.options_boxs[0].x;
opt.options_boxs[i].h = opt.options_boxs[0].h;
opt.options_boxs[i].y = opt.options_boxs[i-1].y + SPACING + opt.options_boxs[i].h;
opt.options_rects[i].w = ((opt.options_boxs[0].w - (SPACING << 1)) / 9) * strlen(opt.option_strings[i]);
opt.options_rects[i].h = opt.options_rects[0].h;
opt.options_rects[i].y = opt.options_boxs[i].y + SPACING;
opt.options_rects[i].x = opt.options_boxs[i].x + SPACING;
}
PRINT_M(INIT OPTIONS)
return;
}
void opt_render_selected()
{
SDL_SetRenderDrawColor(renderer, 32, 64, 32, 255);
SDL_RenderFillRect(renderer, &opt.options_boxs[opt.selected_opt]);
SDL_RenderCopy(renderer, opt.string_texs[opt.selected_opt], NULL, &opt.options_rects[opt.selected_opt]);
}
void render_options()
{
int i;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//render grey boxs for options
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 255);
for(i = 0; i < NUM_OPTIONS; i++)
{
SDL_RenderFillRect(renderer, &opt.options_boxs[i]);
SDL_RenderCopy(renderer, opt.string_texs[i], NULL, &opt.options_rects[i]);
}
opt_render_selected();
SDL_RenderPresent(renderer);
return; return;
} }

1
server
View File

@@ -1 +0,0 @@
build/bin/server

View File

@@ -12,6 +12,11 @@
char buff[PACKSZ]; char buff[PACKSZ];
char pack[PACKSZ], pack2[PACKSZ]; char pack[PACKSZ], pack2[PACKSZ];
void game_server() void game_server()
{ {
IPaddress *ip, _ip, *cip; IPaddress *ip, _ip, *cip;
@@ -125,5 +130,30 @@ int main(void)
} }
int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
*(buff + i) = SDLNet_Read32(src + i);
}
return 1;
}
int write_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz)
{
if(sz%32){
puts("error size must be div by 32");
return 0;
}
for(Uint32 i = 0; i < (sz >> 2); i += 1)
{
SDLNet_Write32(*(src + i), (buff + i));
}
return 1;
}