diff --git a/Makefile b/Makefile index d2c8831..cc1d421 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ ifeq ($(debug),on) CPPFLAGS += -Ddebug endif -BINARIES = game client server +BINARIES = game game_server all : $(BINARIES) @@ -66,10 +66,7 @@ $(OBJ)overworld.o : overworld.c $(INC)overworld.h | $(OBJ) $(OBJ)net.o : net.c $(INC)net.h | $(OBJ) $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS) -$(OBJ)client.o : client.c $(INC)net.h | $(OBJ) - $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS) - -$(OBJ)server.o : server.c $(INC)net.h | $(OBJ) +$(OBJ)game_server.o : game_server.c $(INC)net.h | $(OBJ) $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS) @@ -84,11 +81,8 @@ $(OBJ)server.o : server.c $(INC)net.h | $(OBJ) #$(BIN) : $(OBJ) | $(BIN) # $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -$(BIN)client : $(OBJ)client.o | $(BIN) - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) - -$(BIN)server: $(OBJ)server.o | $(BIN) - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +$(BIN)game_server: $(OBJ)game_server.o $(OBJ)net.o | $(BIN) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(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\ diff --git a/client b/client deleted file mode 120000 index f457900..0000000 --- a/client +++ /dev/null @@ -1 +0,0 @@ -build/bin/client \ No newline at end of file diff --git a/client.c b/client.c index 448742d..a1d3422 100644 --- a/client.c +++ b/client.c @@ -16,10 +16,9 @@ packet msg; int main(void) { - + IPaddress ip; TCPsocket sock; - int ret = 0; SDL_Init(0); diff --git a/game_server b/game_server new file mode 120000 index 0000000..1eb75ab --- /dev/null +++ b/game_server @@ -0,0 +1 @@ +build/bin/game_server \ No newline at end of file diff --git a/net/server.c b/game_server.c similarity index 64% rename from net/server.c rename to game_server.c index d9278ed..6594970 100644 --- a/net/server.c +++ b/game_server.c @@ -6,23 +6,32 @@ #include #include #include +#include -#define MAX_SOCKS 4 -char buff[PACKSZ]; -char pack[PACKSZ], pack2[PACKSZ]; +#define MAX_LOBBIES 16 + + +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() { IPaddress *ip, _ip, *cip; - TCPsocket sock, csock; - TCPsocket sock_arr[MAX_SOCKS]; + TCPsocket sock_arr[MAX_LOBBIES]; ip = &_ip; SDLNet_SocketSet socks; int i; - Uint16 c_port; - Uint32 c_ip; int flag = 1; int ret = 0; @@ -44,24 +53,26 @@ void game_server() 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; } + //main server loop while(flag) { csock = SDLNet_TCP_Accept(sock); if(!csock && SDLNet_CheckSockets(socks, 0) > 0 ){ //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 csock = sock_arr[i]; if(csock == NULL){continue;} if(SDLNet_SocketReady(csock)){ - ret = SDLNet_TCP_Recv(csock, pack, PACKSZ); + ret = SDLNet_TCP_Recv(csock, net_buff, PACKSZ); if(ret < PACKSZ){ printf("TCP recv error: %s\n", SDLNet_GetError()); sock_arr[i] = NULL; @@ -69,8 +80,8 @@ void game_server() SDLNet_TCP_Close(csock); continue; } - read_to_buff((Uint32*)buff, (Uint32*)pack, PACKSZ); - switch(((packet*)buff)->type) + read_from_packet((Uint32*)&recved, (Uint32*)net_buff, PACKSZ); + switch(recved.type) { case G_STATE: PRINT_M("got to get state case"); @@ -83,13 +94,16 @@ void game_server() break; case NEW_HOST: PRINT_M("got to new host state"); + new_host_setup(); break; case JOIN_REQ: PRINT_M("got to join request state"); break; case U_NAME: PRINT_M("got to update name state"); - flag = 0; + break; + default: + printf("unknown packet type: %d\n", recved.type); break; } } @@ -105,28 +119,43 @@ void game_server() if(SDLNet_TCP_AddSocket(socks, csock) <= 0){ 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; } } } } - return; } SDLNet_TCP_Close(sock); SDLNet_Quit(); SDL_Quit(); - } int main(void) { - game_server(); - puts("donzo"); + game_server(); 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; +} diff --git a/host.c b/host.c index 685b71d..661bab7 100644 --- a/host.c +++ b/host.c @@ -118,7 +118,7 @@ char *get_this_name(int i) { 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; } } else{ @@ -217,6 +217,7 @@ void render_main() else if(SDL_PointInRect(&mouse_pos, &host.set_name_rect)){ over_object = SET_NAME_BUTTON; } + else {over_object = NO_OBJECT; } if(lobby_has_name){ render_lobby(); } else{ render_lobby_name(); } diff --git a/include/chat.h b/include/chat.h deleted file mode 100644 index 17f050e..0000000 --- a/include/chat.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - CHAT: A chat client/server using the SDL example network library - Copyright (C) 1997-2022 Sam Lantinga - - 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 diff --git a/include/game.h b/include/game.h index 38edf1f..0afc7f7 100644 --- a/include/game.h +++ b/include/game.h @@ -20,7 +20,6 @@ extern SDL_Point mouse_pos; extern int state; void start_battle(); -void show_options(); #define GRAB_NEW 2 #define GRAB_OLD 1 diff --git a/include/net.h b/include/net.h index 88497bc..c0f7ff7 100644 --- a/include/net.h +++ b/include/net.h @@ -2,12 +2,17 @@ #ifndef __my_net_h__ #define __my_net_h__ +#include +#include + + #define PORT 36911 #define SERVER "localhost" #define PACKSZ 1024 + enum pack_type{ 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)]; } packet; - -int read_to_buff(Uint32 *buff, Uint32 *src, Uint32 sz) +typedef struct host_info { - 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; -} + char name[MAX_NAMESZ]; + IPaddress ip; +}host_info; -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; -} + +int read_from_packet(Uint32 *buff, Uint32 *src, Uint32 sz); +int write_for_sending(Uint32 *buff, Uint32 *src, Uint32 sz); + + +int new_host(char l[MAX_NAMESZ]); #endif diff --git a/include/options.h b/include/options.h new file mode 100644 index 0000000..6c460dc --- /dev/null +++ b/include/options.h @@ -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 diff --git a/input.c b/input.c index b86b831..1d679d6 100644 --- a/input.c +++ b/input.c @@ -6,36 +6,21 @@ #include #include #include +#include +#include +int prev_state; + +void mainmenu_selection_state(); void handle_input_mainmenu(SDL_Event *e); void handle_input_battle(SDL_Event *e); void handle_input_overworld(SDL_Event *e); void handle_input_host(SDL_Event *e); void handle_input_join(SDL_Event *e); +void handle_input_options(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: - state = OPTIONS; - break; - case EXIT_GAME: - running = 0; - break; - } -} - +//main event handler void handle_event(SDL_Event *e){ while(SDL_PollEvent(e)) { @@ -78,6 +63,9 @@ void handle_event(SDL_Event *e){ case JOIN: handle_input_join(e); break; + case OPTIONS: + handle_input_options(e); + break; default: 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) { switch(e->key.keysym.sym){ - case SDLK_ESCAPE: - running = 0; - break; case SDLK_UP: case SDLK_w: if (selected_index) { selected_index--; } @@ -131,7 +139,8 @@ void handle_input_overworld(SDL_Event *e) switch(e->key.keysym.sym) { case SDLK_ESCAPE: - running = 0; + prev_state = state; + state = OPTIONS; break; case SDLK_UP: case SDLK_w: @@ -170,7 +179,8 @@ void handle_input_host(SDL_Event *e) switch(e->key.keysym.sym) { case SDLK_ESCAPE: - running = 0; + PRINT_M(TELL SERVER IM NOT A HOST) + state = TITLE; break; case SDLK_BACKSPACE: PRINT_M(got backspace) @@ -196,8 +206,17 @@ void handle_input_host(SDL_Event *e) strcpy(get_this_name(0), nameptr); break; case START_GAME_BUTTON: - if(strlen(get_this_name(0)) >= 1){ - lobby_has_name = 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; + } + else{ + PRINT_M(ERROR GOT NEG FOR NEW HOST) + } + } + else{//were in the lobby waiting for homies + PRINT_M(START COOP GAME) } break; } @@ -221,7 +240,7 @@ void handle_input_join(SDL_Event *e) switch(e->key.keysym.sym) { case SDLK_ESCAPE: - running = 0; + state = TITLE; break; case SDLK_BACKSPACE: PRINT_M(got backspace) @@ -259,3 +278,58 @@ void handle_input_join(SDL_Event *e) } 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; +} diff --git a/join.c b/join.c index 1d9ada5..1043d1a 100644 --- a/join.c +++ b/join.c @@ -102,6 +102,7 @@ void init_join() PRINT_M(INIT JOIN) + return; } void join_update_hover() diff --git a/main.c b/main.c index 649eef2..806bc35 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -47,6 +48,7 @@ int main(void) init_join(); init_host(); init_overworld(); + init_options(); running = 1; @@ -72,7 +74,7 @@ int main(void) render_join_game(); break; case OPTIONS: - show_options(); + render_options(); break; case TITLE: render_mainmenu(); diff --git a/net.c b/net.c index aeb77a7..4e1a758 100644 --- a/net.c +++ b/net.c @@ -2,7 +2,6 @@ #include #include - #include #include #include @@ -12,15 +11,95 @@ #include #include +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; -static SDLNet_SocketSet socketset = NULL; -static UDPpacket **packets = NULL; -static struct { - int active; - Uint8 name[MAX_NAMESZ]; -} people[MAX_PLAYERS]; + +int new_host(char lobby_name[MAX_NAMESZ]) +{ + int ret; + if(!init_server_connection()){ + return 0; + } + 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; +} + diff --git a/net/Makefile b/net/Makefile deleted file mode 100755 index a9d1fd1..0000000 --- a/net/Makefile +++ /dev/null @@ -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 $< $@ diff --git a/net/client b/net/client deleted file mode 120000 index f457900..0000000 --- a/net/client +++ /dev/null @@ -1 +0,0 @@ -build/bin/client \ No newline at end of file diff --git a/net/mydebug.h b/net/mydebug.h deleted file mode 100644 index 0eaa36b..0000000 --- a/net/mydebug.h +++ /dev/null @@ -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 -#include - -#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 diff --git a/net/net.h b/net/net.h deleted file mode 100644 index 88497bc..0000000 --- a/net/net.h +++ /dev/null @@ -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 diff --git a/net/server b/net/server deleted file mode 120000 index 506e61f..0000000 --- a/net/server +++ /dev/null @@ -1 +0,0 @@ -build/bin/server \ No newline at end of file diff --git a/net/site b/net/site deleted file mode 100644 index 301f0b8..0000000 --- a/net/site +++ /dev/null @@ -1 +0,0 @@ -www.libsdl.org/projects/old/SDL_net/docs/ diff --git a/options.c b/options.c index 1b0ea7c..f4e668c 100644 --- a/options.c +++ b/options.c @@ -2,14 +2,124 @@ #include #include +#include #include #include #include #include #include +#include -void show_options(){ - running = 0; +#define SPACING 20 + +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; } diff --git a/server b/server deleted file mode 120000 index 506e61f..0000000 --- a/server +++ /dev/null @@ -1 +0,0 @@ -build/bin/server \ No newline at end of file diff --git a/server.c b/server.c index 1b80504..f8e5856 100644 --- a/server.c +++ b/server.c @@ -12,6 +12,11 @@ char buff[PACKSZ]; char pack[PACKSZ], pack2[PACKSZ]; + + + + + void game_server() { 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; +}