Compare commits

..

10 Commits

51 changed files with 5743 additions and 95 deletions

1
.gitignore vendored
View File

@@ -467,7 +467,6 @@ CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json

93
Makefile Normal file
View File

@@ -0,0 +1,93 @@
#Troy Rosin
CC =gcc
CFLAGS =-g
CPPFLAGS =-c -g -Wall -pedantic -Wextra
LDFLAGS =-lSDL2 -lm -lSDL2main -lSDL2_image
LDFLAGS +=-L/usr/local/lib -lSDL2_ttf -lSDL2_net
INCFLAGS = -I/usr/local/include/SDL2 -Iinclude
INC =include/
BIN =build/bin/
LIB =build/lib/
OBJ =build/obj/
DIRS =$(BIN) $(LIB) $(OBJ)
debug ?=on
ifeq ($(debug),on)
CPPFLAGS += -Ddebug
endif
BINARIES = game game_server
all : $(BINARIES)
clean :
rm -rf build/ $(BINARIES)
$(DIRS) :
mkdir -p $@
## --- OBJECT FILES ---
#$(OBJ) : | $(OBJ)
# $(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)client_test.o : client_test.c | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)server_test.o : server_test.c | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)main.o : main.c $(INC)input.h $(INC)game.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)battle.o : battle.c $(INC)game.h $(INC)deck.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)input.o : input.c $(INC)input.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)host.o : host.c $(INC)host.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)join.o : join.c | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)options.o : options.c | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)game.o : game.c $(INC)game.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)overworld.o : overworld.c $(INC)overworld.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)net.o : net.c $(INC)net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
$(OBJ)game_server.o : game_server.c $(INC)net.h | $(OBJ)
$(CC) $(CPPFLAGS) -o $@ $< $(INCFLAGS)
## --- LIBRARY FILES ---
#$(LIB) : | $(LIB)
# ar rcs $@ $^
## --- EXECUTABLE FILES ---
#$(BIN) : $(OBJ) | $(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\
| $(BIN)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(BINARIES) : % : $(BIN)%
ln -sf $< $@

View File

@@ -1,3 +1,53 @@
# shitgame
game made using only sdl2 and c
DEPENDANCIES
SDL CORE:
use SDL 2.24.x
git clone https://github.com/libsdl-org/SDL.git
cd SDL
git checkout release-2.24.x
mkdir build
cd build
cmake .. -DCMAKE_POLICY_VERSION_MINIMUM=3.5
sudo make install -j
SDL IMAGE:
use SDL_Image 2.8.x
git clone https://github.com/libsdl-org/SDL_image.git
cd SDL_image
git checkout release-2.8.x
mkdir build
cd build
cmake ..
sudo make install
SDL_Net:
use SDL_Net 2.2.x
git clone https://github.com/libsdl-org/SDL_net.git
cd SDL_net
git checkout release-2.2.x
mkdir build
cd build
cmake ..
sudo make install
SDL_ttf
use SDL_ttf 2.24.x
git clone https://github.com/libsdl-org/SDL_ttf.git
cd SDL_ttf
git checkout release-2.24.x
mkdir build
cd build
cmake ..
sudo make install

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 10 KiB

BIN
assets/boss_overworld.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
assets/chest_overworld.ase Normal file

Binary file not shown.

BIN
assets/chest_overworld.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
assets/enemy_grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 208 B

BIN
assets/exit_text.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
assets/font.ttf Normal file

Binary file not shown.

BIN
assets/friendly_far.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 B

BIN
assets/friendly_grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 B

BIN
assets/host_text.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
assets/mainscreen.ase Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 11 KiB

BIN
assets/ow_grid_grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
assets/ow_selected_grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 898 B

BIN
assets/player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
assets/skull_overworld.ase Normal file

Binary file not shown.

BIN
assets/skull_overworld.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
assets/start_text.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

102
battle.c
View File

@@ -6,6 +6,8 @@
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <battle.h>
#define BOARDER_THICKNESS 10
//do not let dx or dy be anything less than 2
@@ -15,23 +17,28 @@
SDL_Rect mouse_rect;
SDL_Rect bot_rect, hand_rect;
SDL_Point mouse_pos;
int check_col(int i);
int selected_card;
int dragging;
void start_battle(){
int cardoff_x, cardoff_y;
int i, cardoff_x, cardoff_y;
int mx, my;
state = BATTLE;
selected_card = -1;
dragging = 0;
PRINT_M(STARTING GAME LOOP);
//SDL_Texture *maroon = IMG_LoadTexture(renderer, "assets/maroon.png");
SDL_Texture *grid = IMG_LoadTexture(renderer, "assets/battle_grid.png");
SDL_Texture *card = IMG_LoadTexture(renderer, "assets/default_card.png");
SDL_Texture *selected = IMG_LoadTexture(renderer, "assets/selected_card.png");
SDL_Texture *enemy_grid = IMG_LoadTexture(renderer, "assets/enemy_grid.png");
SDL_Texture *friendly_grid = IMG_LoadTexture(renderer, "assets/friendly_grid.png");
SDL_Texture *friendly_far = IMG_LoadTexture(renderer, "assets/friendly_far.png");
SDL_Texture *selected_grid = IMG_LoadTexture(renderer, "assets/selected_grid.png");
mouse_rect.x = 0;
mouse_rect.y = 0;
@@ -61,12 +68,55 @@ void start_battle(){
HAND hand;
hand.handsize = 7;
for(int i=0; i<7; i++){
for(i=0; i<7; i++){
hand.cards[i] = card;
hand.card_rects[i].w = CARD_SIZE;
hand.card_rects[i].h = CARD_SIZE;
}
battle_rows[0] = enemy_backrow;
battle_rows[1] = enemy_frontrow;
battle_rows[2] = friendly_frontrow;
battle_rows[3] = friendly_midrow;
battle_rows[4] = friendly_backrow;
battle_rows[5] = friendly_farrow;
//battle grid setup
//enemy setup
int grid_w, grid_h;
int side_buffer = DEF_WINDOW_WIDTH / (BATTLE_COLS + 2);
enemy_frontrow[0].x = enemy_backrow[0].x = side_buffer;
enemy_frontrow[0].w = grid_w = enemy_backrow[0].w = side_buffer;
enemy_backrow[0].y = GRID_SPACING;
enemy_frontrow[0].h = grid_h = enemy_backrow[0].h = side_buffer - (side_buffer >> 2 );
enemy_frontrow[0].y = (GRID_SPACING * 2) + grid_h;
for(i = 1; i < BATTLE_COLS; i++)
{
enemy_frontrow[i].x = enemy_backrow[i].x = enemy_backrow[i-1].x + grid_w + GRID_SPACING;
enemy_frontrow[i].w = enemy_backrow[i].w = grid_w;
enemy_backrow[i].y = GRID_SPACING;
enemy_frontrow[i].h = enemy_backrow[i].h = grid_h;
enemy_frontrow[i].y = enemy_frontrow[0].y;
}
//friendly side setup
for(i=0; i < BATTLE_COLS; i++)
{
friendly_farrow[i].x = friendly_backrow[i].x = friendly_midrow[i].x =
friendly_frontrow[i].x = enemy_frontrow[i].x;
friendly_farrow[i].w = friendly_backrow[i].w = friendly_midrow[i].w =
friendly_frontrow[i].w = grid_w;
friendly_backrow[i].h = friendly_midrow[i].h =
friendly_frontrow[i].h = grid_h;
friendly_farrow[i].h = grid_h >> 1;
friendly_frontrow[i].y = enemy_frontrow[i].y + grid_h + (GRID_SPACING * 3);
friendly_midrow[i].y = friendly_frontrow[i].y + grid_h + GRID_SPACING;
friendly_backrow[i].y = friendly_midrow[i].y + grid_h + GRID_SPACING;
friendly_farrow[i].y = friendly_backrow[i].y + grid_h + GRID_SPACING;
}
int check = 1;
int inside = 0;
while(running)
{
handle_event(&event);
@@ -74,7 +124,6 @@ void start_battle(){
SDL_GetMouseState(&mx, &my);
mouse_pos.x = mouse_rect.x = mx;
mouse_pos.y = mouse_rect.y = my;
int inside = 0;
//handle resizing hand box
inside = SDL_PointInRect(&mouse_pos, &hand_rect);
if(SDL_GetTicks64() > lastdraw)
@@ -107,14 +156,13 @@ void start_battle(){
{
spacing = useable_hand_space / (hand.handsize - 1);
}
int check = 1;
check = 1;
if(selected_card == -1) { check = 1;}
else if(SDL_PointInRect(&mouse_pos, &(hand.card_rects[selected_card]))){ check = 0;}
else { selected_card = -1; }
for(int i = 0; i < hand.handsize; i++)
for(i = 0; i < hand.handsize; i++)
{
hand.card_rects[i].x = hand_rect.x + BOARDER_THICKNESS + ( spacing * i)
- ((CARD_SIZE - CARD_W)>>1) ;
hand.card_rects[i].x = hand_rect.x + BOARDER_THICKNESS + ( spacing * i);
hand.card_rects[i].y = hand_rect.y + BOARDER_THICKNESS;
if(check && SDL_PointInRect(&mouse_pos, &(hand.card_rects[i])))
{
@@ -147,7 +195,7 @@ void start_battle(){
if(!inside)
{
hand.handsize--;
for(int i = selected_card; i < hand.handsize; i++)
for(i = selected_card; i < hand.handsize; i++)
{
hand.cards[i] = hand.cards[i+1];
}
@@ -156,9 +204,30 @@ void start_battle(){
}
}
}
SDL_RenderClear(renderer);
//background
//background this isnt a grid anymore
SDL_RenderCopy(renderer, grid, NULL, NULL);
//render battle grid
check = 1;
int sel_row = 0;
for(i = 0; i < BATTLE_COLS; i++)
{
SDL_RenderCopy(renderer, enemy_grid, NULL, &(enemy_backrow[i]));
SDL_RenderCopy(renderer, enemy_grid, NULL, &(enemy_frontrow[i]));
SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_frontrow[i]));
SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_midrow[i]));
SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_backrow[i]));
SDL_RenderCopy(renderer, friendly_far, NULL, &(friendly_farrow[i]));
if(check && (sel_row = check_col(i)))
{
check = 0;
SDL_RenderCopy(renderer, selected_grid, NULL, &(battle_rows[sel_row-1][i]));
}
}
//bottom box for ui shit
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
@@ -196,6 +265,15 @@ void start_battle(){
return;
}
//bad but im lazy lol fix in future
int check_col(int i){
int ret = SDL_PointInRect(&mouse_pos, &(enemy_backrow[i])) * 1;
ret |= SDL_PointInRect(&mouse_pos, &(enemy_frontrow[i])) * 2;
ret |= SDL_PointInRect(&mouse_pos, &(friendly_frontrow[i])) * 3;
ret |= SDL_PointInRect(&mouse_pos, &(friendly_midrow[i])) * 4;
ret |= SDL_PointInRect(&mouse_pos, &(friendly_backrow[i])) * 5;
ret |= SDL_PointInRect(&mouse_pos, &(friendly_farrow[i])) * 6;
return ret;
}

91
client.c Normal file
View File

@@ -0,0 +1,91 @@
//troy Rosin
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#define SECS 1
char buf[PACKSZ];
char pack[PACKSZ];
packet msg;
int main(void)
{
IPaddress ip;
TCPsocket sock;
SDL_Init(0);
SDLNet_Init();
if(SDLNet_ResolveHost(&ip, SERVER, PORT) == -1)
{
printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
exit(1);
}
sock = SDLNet_TCP_Open(&ip);
if(!sock)
{
printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
exit(1);
}
PRINT_M("starting");
msg.type = G_STATE;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
PRINT_M("packed up!");
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = S_STATE;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = HOSTS_REQ;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = NEW_HOST;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = JOIN_REQ;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
msg.type = U_NAME;
write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
SDLNet_TCP_Send(sock, pack, PACKSZ);
PRINT_M("sent");
sleep(SECS);
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
puts("donzo");
return 0;
}

99
client_test.c Normal file
View File

@@ -0,0 +1,99 @@
/*
** client.c -- a stream socket client demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT "3490" // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
inet_ntop(p->ai_family,
get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: attempting connection to %s\n", s);
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
perror("client: connect");
close(sockfd);
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family,
get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: connected to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("client: received '%s'\n",buf);
close(sockfd);
return 0;
}

15
deck.c Normal file
View File

@@ -0,0 +1,15 @@
/*Troy Rosin*/
#include <deck.h>
#include <game.h>
#include <input.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>

1
export.sh Executable file
View File

@@ -0,0 +1 @@
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

1
game Symbolic link
View File

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

19
game.c
View File

@@ -2,8 +2,27 @@
#include <game.h>
#include <input.h>
#include <overworld.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>
void init_start_game()
{
PRINT_M(INIT START GAME)
}
void render_start_game()
{
state = OVERWORLD;
return;
}

1
game_server Symbolic link
View File

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

161
game_server.c Normal file
View File

@@ -0,0 +1,161 @@
//troy Rosin
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#include <game.h>
#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_arr[MAX_LOBBIES];
ip = &_ip;
SDLNet_SocketSet socks;
int i;
int flag = 1;
int ret = 0;
SDL_Init(0);
SDLNet_Init();
if(SDLNet_ResolveHost(ip, NULL, PORT) == -1)
{
printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
exit(1);
}
sock = SDLNet_TCP_Open(ip);
if(!sock)
{
printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
exit(1);
}
socks = SDLNet_AllocSocketSet(MAX_LOBBIES);
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_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, net_buff, PACKSZ);
if(ret < PACKSZ){
printf("TCP recv error: %s\n", SDLNet_GetError());
sock_arr[i] = NULL;
SDLNet_TCP_DelSocket(socks, csock);
SDLNet_TCP_Close(csock);
continue;
}
read_from_packet((Uint32*)&recved, (Uint32*)net_buff, PACKSZ);
switch(recved.type)
{
case G_STATE:
PRINT_M("got to get state case");
break;
case S_STATE:
PRINT_M("got to set state case");
break;
case HOSTS_REQ:
PRINT_M("got to hosts request state");
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");
break;
default:
printf("unknown packet type: %d\n", recved.type);
break;
}
}
}
}
else if (csock) {
cip = SDLNet_TCP_GetPeerAddress(csock);
if(!cip){
printf("get peer addr error: %s\n", SDLNet_GetError());
}
else {
PRINT_M("we got a new connection!");
if(SDLNet_TCP_AddSocket(socks, csock) <= 0){
printf("add sock to set error: %s\n", SDLNet_GetError());
}
for(i = 0; i < MAX_LOBBIES; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
}
}
}
}
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
}
int main(void)
{
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;
}

238
host.c Normal file
View File

@@ -0,0 +1,238 @@
/*Troy Rosin*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <host.h>
#include <err_check.h>
#define BOARDER_THICKNESS 20
int over_object;
int num_players = 1;
int lobby_has_name = 0;
struct host_ctx
{
char player_names[MAX_PLAYERS][MAX_NAMESZ];
char temp_name[MAX_NAMESZ];
char lobby_name[MAX_NAMESZ];
char lobby_name_perm[MAX_NAMESZ];
SDL_Rect set_name_rect, name_rect, player_name_rect;
SDL_Rect player_rects[MAX_PLAYERS];
SDL_Rect player_name_rects[MAX_PLAYERS];
SDL_Rect start_rect;
TTF_Font* font;
SDL_Surface *set_name_surf;
SDL_Surface *start_surf;
SDL_Surface *lobby_name_surf;
SDL_Texture *set_name_tex;
SDL_Texture *lobby_name_tex;
SDL_Texture *start_tex;
SDL_Surface *player_name_surfs[MAX_PLAYERS];
SDL_Surface *player_tempname_surf;
SDL_Texture *player_name_texs[MAX_PLAYERS];
SDL_Texture *player_tempname_tex;
};
struct host_ctx host;
void init_host()
{
int i;
over_object = NO_OBJECT;
strcpy(host.player_names[0], "player_1");
strcpy(host.player_names[1], "no_player_2");
strcpy(host.player_names[2], "no_player_3");
strcpy(host.player_names[3], "no_player_4");
host.font = TTF_OpenFont("assets/font.ttf", 24);
host.set_name_surf = TTF_RenderText_Solid(host.font, "set name", white);
host.start_surf = TTF_RenderText_Solid(host.font, "start game", white);
host.lobby_name_surf = TTF_RenderText_Solid(host.font, host.lobby_name, white);
host.set_name_tex = SDL_CreateTextureFromSurface(renderer, host.set_name_surf);
host.start_tex = SDL_CreateTextureFromSurface(renderer, host.start_surf);
host.set_name_rect.w = DEF_WINDOW_WIDTH >> 3;
host.set_name_rect.h = DEF_WINDOW_HEIGHT >> 4;
host.set_name_rect.x = DEF_WINDOW_WIDTH - (host.set_name_rect.w << 1);
host.set_name_rect.y = DEF_WINDOW_HEIGHT - (host.set_name_rect.h << 2);
host.name_rect.w = DEF_WINDOW_WIDTH - (host.set_name_rect.w << 2) - (BOARDER_THICKNESS << 1);
host.name_rect.h = host.set_name_rect.h;
host.name_rect.x = (host.set_name_rect.w << 1) + BOARDER_THICKNESS;
host.name_rect.y = host.set_name_rect.y;
host.start_rect.w = host.set_name_rect.w;
host.start_rect.h = host.set_name_rect.h;
host.start_rect.x = (DEF_WINDOW_WIDTH >> 1) - (host.start_rect.w >> 1);
host.start_rect.y = host.set_name_rect.y + host.set_name_rect.h + BOARDER_THICKNESS;
host.player_name_rect.w = DEF_WINDOW_WIDTH - (host.set_name_rect.w << 2) - (BOARDER_THICKNESS << 1);
host.player_name_rect.h = host.set_name_rect.h;
host.player_name_rect.x = (host.set_name_rect.w << 1) + BOARDER_THICKNESS;
host.player_name_rect.y = host.set_name_rect.y;
host.player_rects[0].w = host.player_rects[1].w = host.player_rects[2].w = host.player_rects[3].w
= DEF_WINDOW_WIDTH - (host.set_name_rect.w << 1);
host.player_rects[0].h = host.player_rects[1].h = host.player_rects[2].h = host.player_rects[3].h
= ((DEF_WINDOW_HEIGHT - (DEF_WINDOW_HEIGHT - host.set_name_rect.y))
- (BOARDER_THICKNESS << 2) - BOARDER_THICKNESS) >> 2;
host.player_rects[0].x = host.player_rects[1].x = host.player_rects[2].x = host.player_rects[3].x
= host.set_name_rect.w;
host.player_rects[0].y = BOARDER_THICKNESS;
host.player_rects[1].y = host.player_rects[0].y + host.player_rects[0].h + BOARDER_THICKNESS;
host.player_rects[2].y = host.player_rects[1].y + host.player_rects[1].h + BOARDER_THICKNESS;
host.player_rects[3].y = host.player_rects[2].y + host.player_rects[2].h + BOARDER_THICKNESS;
for(i = 0; i < MAX_PLAYERS; i++)
{
host.player_name_rects[i].w = host.player_rects[i].w;
host.player_name_rects[i].h = host.player_rects[i].h - (BOARDER_THICKNESS << 1);
host.player_name_rects[i].x = host.player_rects[i].x + BOARDER_THICKNESS;
host.player_name_rects[i].y = host.player_rects[i].y + BOARDER_THICKNESS;
}
return;
}
char *get_this_name(int i)
{
if(!lobby_has_name) {
if(i == -1){ return host.lobby_name; }
if(i == 0) { return host.lobby_name_perm; }
}
else{
if(i == -1) { return host.temp_name; }
if(i >= 0 && i < MAX_PLAYERS){
return host.player_names[i];
}
}
return NULL;
}
void render_lobby()
{
int i;
host.player_tempname_surf= TTF_RenderText_Solid(host.font, host.temp_name, white);
host.player_tempname_tex = SDL_CreateTextureFromSurface(renderer, host.player_tempname_surf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//set name button
SDL_SetRenderDrawColor(renderer, 64, 64, 64,255);
SDL_RenderFillRect(renderer, &host.set_name_rect);
SDL_RenderCopy(renderer, host.set_name_tex, NULL, &(host.set_name_rect));
//player fields
for(i = 0; i < MAX_PLAYERS; i++)
{
host.player_name_rects[i].w = (host.player_rects[0].w / MAX_NAMESZ) * strlen(host.player_names[i]);
host.player_name_surfs[i] = TTF_RenderText_Solid(host.font, host.player_names[i], white);
host.player_name_texs[i] = SDL_CreateTextureFromSurface(renderer, host.player_name_surfs[i]);
SDL_RenderFillRect(renderer, &(host.player_rects[i]));
SDL_RenderCopy(renderer, host.player_name_texs[i], NULL, &(host.player_name_rects[i]));
}
//name field
host.player_name_rect.w = (host.name_rect.w / MAX_NAMESZ) * strlen(host.temp_name);
SDL_RenderFillRect(renderer, &host.name_rect);
SDL_RenderCopy(renderer, host.player_tempname_tex, NULL, &host.player_name_rect);
//start game button
SDL_RenderFillRect(renderer, &host.start_rect);
SDL_RenderCopy(renderer, host.start_tex, NULL, &(host.start_rect));
SDL_RenderPresent(renderer);
host.player_tempname_surf= TTF_RenderText_Solid(host.font, host.temp_name, white);
host.player_tempname_tex = SDL_CreateTextureFromSurface(renderer, host.player_tempname_surf);
SDL_FreeSurface(host.player_tempname_surf);
SDL_DestroyTexture(host.player_tempname_tex);
for(i = 0; i < MAX_PLAYERS; i++)
{
SDL_FreeSurface(host.player_name_surfs[i]);
SDL_DestroyTexture(host.player_name_texs[i]);
}
}
void render_lobby_name()
{
host.lobby_name_surf = TTF_RenderText_Solid(host.font, host.lobby_name, white);
host.lobby_name_tex = SDL_CreateTextureFromSurface(renderer, host.lobby_name_surf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//set lobby name button
SDL_SetRenderDrawColor(renderer, 64, 64, 64,255);
SDL_RenderFillRect(renderer, &host.set_name_rect);
SDL_RenderCopy(renderer, host.set_name_tex, NULL, &(host.set_name_rect));
//name field
host.player_name_rect.w = (host.name_rect.w / MAX_NAMESZ) * strlen(host.lobby_name);
SDL_RenderFillRect(renderer, &host.name_rect);
SDL_RenderCopy(renderer, host.lobby_name_tex, NULL, &host.player_name_rect);
//start game button
SDL_RenderFillRect(renderer, &host.start_rect);
SDL_RenderCopy(renderer, host.start_tex, NULL, &(host.start_rect));
SDL_RenderPresent(renderer);
}
void render_main()
{
SDL_SetTextInputRect(&host.name_rect);
SDL_GetMouseState(&(mouse_pos.x), &(mouse_pos.y));
if(SDL_PointInRect(&mouse_pos, &host.start_rect)){
over_object = START_GAME_BUTTON;
}
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(); }
}
void render_host_game(){
render_main();
return;
}

1042
include/SDL_net.h Normal file

File diff suppressed because it is too large Load Diff

2343
include/SDL_ttf.h Normal file

File diff suppressed because it is too large Load Diff

94
include/SDLnetsys.h Normal file
View File

@@ -0,0 +1,94 @@
/*
SDL_net: An example cross-platform network library for use with SDL
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.
*/
/* Include normal system headers */
#if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE)
#define _DARWIN_C_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(__OS2__) && !defined(__EMX__)
#include <nerrno.h>
#else
#include <errno.h>
#endif
/* Include system network headers */
#if defined(__WIN32__) || defined(WIN32)
#define __USE_W32_SOCKETS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#else /* UNIX */
#ifdef __OS2__
#include <sys/param.h>
#endif
#include <sys/types.h>
#ifdef __FreeBSD__
#include <sys/socket.h>
#endif
#include <sys/ioctl.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#ifndef __BEOS__
#include <arpa/inet.h>
#endif
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netdb.h>
#endif /* WIN32 */
#ifdef __OS2__
typedef int socklen_t;
#elif 0
/* FIXME: What platforms need this? */
typedef Uint32 socklen_t;
#endif
/* System-dependent definitions */
#ifndef __USE_W32_SOCKETS
#ifdef __OS2__
#define closesocket soclose
#else /* !__OS2__ */
#define closesocket close
#endif /* __OS2__ */
#define SOCKET int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif /* __USE_W32_SOCKETS */
#ifdef __USE_W32_SOCKETS
#define SDLNet_GetLastError WSAGetLastError
#define SDLNet_SetLastError WSASetLastError
#ifndef EINTR
#define EINTR WSAEINTR
#endif
#else
int SDLNet_GetLastError(void);
void SDLNet_SetLastError(int err);
#endif

22
include/battle.h Normal file
View File

@@ -0,0 +1,22 @@
//Troy Rosin
#ifndef __battle_h__
#define __battle_h__
#include <SDL2/SDL.h>
#define GRID_SPACING 4
#define BATTLE_COLS 7
#define BATTLE_ROWS 6
SDL_Rect *battle_rows[BATTLE_ROWS];
SDL_Rect enemy_backrow[BATTLE_COLS];
SDL_Rect enemy_frontrow[BATTLE_COLS];
SDL_Rect friendly_frontrow[BATTLE_COLS];
SDL_Rect friendly_midrow[BATTLE_COLS];
SDL_Rect friendly_backrow[BATTLE_COLS];
SDL_Rect friendly_farrow[BATTLE_COLS];
#endif

View File

@@ -9,6 +9,32 @@
#define CARD_SIZE (128 * 2)
#define CARD_W (96 * 2)
#define MAX_CARDS 1 << 8
#define MAX_EFFECTS 4
#define MAX_ENCHANTS 4
#define MAX_NAME 64
#define R_COMMON 1
#define R_UNCOMMON 2
#define R_RARE 3
#define R_ANCIENT 4
#define R_MYTHICAL 5
#define R_DEVINE 6
typedef struct crd
{
SDL_Texture *texture;
int effect[MAX_EFFECTS];
int enchants[MAX_ENCHANTS];
int rarity;
int set;
int type;
char *explination;
char name[MAX_NAME];
struct crd *next;
} CARD;
typedef struct hand
{
int handsize;
@@ -17,6 +43,21 @@ typedef struct hand
} HAND;
typedef struct discard_pile
{
CARD *bot;
CARD *top;
int count;
CARD cards[MAX_CARDS];
} D_PILE;
typedef struct dck
{
int count;
CARD *bot;
CARD *top;
} DECK;
#endif

19
include/err_check.h Normal file
View File

@@ -0,0 +1,19 @@
//Troy Rosin
#ifndef __err_check_h__
#define __err_check_h__
//pass TEX is an SDL_Texture pointer, rend is the renderer
// and path is the path to the texture to load.
#define IMG_GETANDCHECK(TEX, REND, PATH)\
TEX = IMG_LoadTexture(REND, PATH);\
if(!TEX){\
printf("IMG_LoadTexture error: %s\n", IMG_GetError());\
}
#endif

View File

@@ -3,35 +3,33 @@
#ifndef __game_h__
#define __game_h__
void init_start_game();
void render_start_game();
#include <SDL2/SDL.h>
#define DEF_WINDOW_WIDTH 1920
#define DEF_WINDOW_HEIGHT 1080
extern int running;
extern int state;
extern SDL_Point mouse_pos;
#define TITLE 0
#define BATTLE 1
#define START 1
#define HOST 2
#define JOIN 3
#define OPTIONS 4
#define BATTLE 5
#define OVERWORLD 6
extern int state;
#define START_GAME 0
#define EXIT_GAME 1
#define MAIN_SELECTIONS 2
extern int selected_index;
void start_battle();
extern SDL_Window* window;
extern SDL_Renderer* renderer;
extern SDL_Event event;
#define GRAB_NEW 2
#define GRAB_OLD 1
#define LETGO 3
#define NOT_DRAG 0
extern int dragging;
#define MAX_NAMESZ 64
#define MAX_PLAYERS 4
#endif

33
include/host.h Normal file
View File

@@ -0,0 +1,33 @@
//Troy Rosin
#ifndef __host_h__
#define __host_h__
#define NO_OBJECT 0
//host view
#define SET_NAME_BUTTON 1
#define START_GAME_BUTTON 2
//join view
#define JOIN_GAME_BUTTON 1
#define PLAYER_NAME_BOX 2
#define LOBBY_NAME_BOX 3
#define PLAYER_NAME_SEL 0
#define LOBBY_NAME_SEL 1
//host
void render_host_game();
void init_host();
extern int over_object;
extern int lobby_has_name;
char *get_this_name(int i);
//join
char *join_get_name_ptr();
void render_join_game();
void init_join();
int join_over_object();
void join_set_selected(int);
#endif

31
include/main.h Normal file
View File

@@ -0,0 +1,31 @@
//Troy Rosin
#ifndef __main_h__
#define __main_h__
#include <SDL2/SDL.h>
#define DEF_WINDOW_WIDTH 1024
#define DEF_WINDOW_HEIGHT 768
extern int running;
extern int state;
#define START_GAME 0
#define HOST_GAME 1
#define JOIN_GAME 2
#define OPTIONS_MENU 3
#define EXIT_GAME 4
#define MAIN_SELECTIONS 5
extern int selected_index;
extern SDL_Window* window;
extern SDL_Renderer* renderer;
extern SDL_Event event;
extern SDL_Color white;
extern SDL_Point mouse_pos;
#endif

39
include/net.h Normal file
View File

@@ -0,0 +1,39 @@
//Troy Rosin
#ifndef __my_net_h__
#define __my_net_h__
#include <SDL_net.h>
#include <game.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;
typedef struct host_info
{
char name[MAX_NAMESZ];
IPaddress ip;
}host_info;
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

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

22
include/overworld.h Normal file
View File

@@ -0,0 +1,22 @@
//Troy Rosin
#ifndef __overworld_h__
#define __overworld_h__
#include <SDL2/SDL.h>
void init_overworld();
void render_overworld();
#define OW_GRIDSZ 128
typedef struct player
{
SDL_Point pos;
SDL_Texture *texture;
} PLAYER;
PLAYER *get_this_player();
#endif

282
input.c
View File

@@ -2,13 +2,25 @@
#include <SDL2/SDL.h>
#include <game.h>
#include <main.h>
#include <overworld.h>
#include <host.h>
#include <mydebug.h>
#include <options.h>
#include <net.h>
extern int running;
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);
//main event handler
void handle_event(SDL_Event *e){
while(SDL_PollEvent(e))
{
@@ -19,6 +31,10 @@ void handle_event(SDL_Event *e){
{
handle_input_mainmenu(e);
}
if(e->type == SDL_MOUSEBUTTONDOWN)
{
mainmenu_selection_state();
}
break;
case BATTLE:
switch (e->type)
@@ -33,33 +49,74 @@ void handle_event(SDL_Event *e){
dragging = LETGO;
}
break;
case OVERWORLD:
switch(e->type)
{
case SDL_KEYDOWN:
handle_input_overworld(e);
break;
}
break;
case HOST:
handle_input_host(e);
break;
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)
{
running = 0;
}
}
return;
}
}
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--; }
else { selected_index = MAIN_SELECTIONS - 1; }
selected_index %= MAIN_SELECTIONS;
break;
case SDLK_DOWN:
case SDLK_s:
selected_index++;
selected_index %= MAIN_SELECTIONS;
break;
case SDLK_RETURN:
if(selected_index == EXIT_GAME){
running = 0;
}
else{
state = BATTLE;
}
mainmenu_selection_state();
break;
}
return;
}
@@ -72,8 +129,207 @@ void handle_input_battle(SDL_Event *e)
case SDLK_ESCAPE:
running = 0;
break;
default:
printf("%d", (char)e->key.keysym.sym);
}
return;
}
void handle_input_overworld(SDL_Event *e)
{
PLAYER *plr = get_this_player();
switch(e->key.keysym.sym)
{
case SDLK_ESCAPE:
prev_state = state;
state = OPTIONS;
break;
case SDLK_UP:
case SDLK_w:
plr->pos.y -= 1;
break;
case SDLK_DOWN:
case SDLK_s:
plr->pos.y += 1;
break;
case SDLK_RIGHT:
case SDLK_d:
plr->pos.x += 1;
break;
case SDLK_LEFT:
case SDLK_a:
plr->pos.x -= 1;
break;
}
return;
}
void handle_input_host(SDL_Event *e)
{
int oldlen, inputlen;
char tempname[MAX_NAMESZ], *nameptr;
nameptr = get_this_name(-1);
strcpy(tempname, nameptr);
oldlen = strlen(tempname);
switch(e->type)
{
case SDL_KEYUP:
break;
case SDL_KEYDOWN:
switch(e->key.keysym.sym)
{
case SDLK_ESCAPE:
PRINT_M(TELL SERVER IM NOT A HOST)
state = TITLE;
break;
case SDLK_BACKSPACE:
PRINT_M(got backspace)
if(oldlen == 0){ break; }
tempname[oldlen - 1] = '\0';
strcpy(nameptr, tempname);
break;
}
break;
case SDL_TEXTINPUT:
inputlen = strlen(e->text.text);
PRINT_M(got text input event)
if(oldlen + inputlen > MAX_NAMESZ -1){ break; }
strcat(tempname, e->text.text);
strcpy(nameptr, tempname);
break;
case SDL_MOUSEBUTTONDOWN:
switch(over_object)
{
case NO_OBJECT:
break;
case SET_NAME_BUTTON:
strcpy(get_this_name(0), nameptr);
break;
case START_GAME_BUTTON:
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;
}
}
return;
}
void handle_input_join(SDL_Event *e)
{
int oldlen, inputlen;
char tempname[MAX_NAMESZ], *nameptr;
nameptr = join_get_name_ptr();
strcpy(tempname, nameptr);
oldlen = strlen(tempname);
switch(e->type)
{
case SDL_KEYUP:
break;
case SDL_KEYDOWN:
switch(e->key.keysym.sym)
{
case SDLK_ESCAPE:
state = TITLE;
break;
case SDLK_BACKSPACE:
PRINT_M(got backspace)
if(oldlen == 0){ break; }
tempname[oldlen - 1] = '\0';
strcpy(nameptr, tempname);
break;
}
break;
case SDL_TEXTINPUT:
inputlen = strlen(e->text.text);
PRINT_M(got text input event)
if(oldlen + inputlen > MAX_NAMESZ -1){ break; }
strcat(tempname, e->text.text);
strcpy(nameptr, tempname);
break;
case SDL_MOUSEBUTTONDOWN:
switch(join_over_object())
{
case NO_OBJECT:
break;
case JOIN_GAME_BUTTON:
PRINT_M(JOIN_GAME BUTTON)
//check name field and send it off bb
break;
case LOBBY_NAME_BOX:
join_set_selected(LOBBY_NAME_SEL);
break;
case PLAYER_NAME_BOX:
join_set_selected(PLAYER_NAME_SEL);
break;
}
break;
}
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;
}

171
join.c Normal file
View File

@@ -0,0 +1,171 @@
/*Troy Rosin*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>
#include <mydebug.h>
#include <game.h>
#include <host.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#define BOARDER_THICKNESS 20
struct join_ctx
{
int selected;
int over_object;
char player_name[MAX_NAMESZ];
char lobby_name[MAX_NAMESZ];
TTF_Font *font;
SDL_Rect lobby_name_rect, player_name_rect, join_game_rect;
SDL_Rect lobby_name_box, player_name_box, join_game_box;
SDL_Surface *player_name_surf, *lobby_prompt_surf, *name_promt_surf,
*join_surf, *lobby_name_surf;
SDL_Texture *player_name_tex, *lobby_prompt_tex, *name_prompt_tex,
*lobby_name_tex, *join_tex;
};
struct join_ctx join;
void join_set_selected(int obj)
{
join.selected = obj;
return;
}
int join_over_object()
{
return join.over_object;
}
char *join_get_name_ptr()
{
if(join.selected == PLAYER_NAME_SEL){
return join.player_name;
}
else if(join.selected == LOBBY_NAME_SEL){
return join.lobby_name;
}
return NULL;
}
void init_join()
{
join.over_object = NO_OBJECT;
join.font = TTF_OpenFont("assets/font.ttf", 24);
strcpy(join.player_name, "PLAYER");
strcpy(join.lobby_name, "lobby_name");
join.player_name_box.x = DEF_WINDOW_WIDTH >> 3;
join.player_name_box.y = DEF_WINDOW_HEIGHT >> 3;
join.player_name_box.w = (DEF_WINDOW_WIDTH >> 1) + (DEF_WINDOW_WIDTH >> 2);
join.player_name_box.h = DEF_WINDOW_HEIGHT >> 3;
join.player_name_rect.x = join.player_name_box.x + BOARDER_THICKNESS;
join.player_name_rect.y = join.player_name_box.y + BOARDER_THICKNESS;
join.player_name_rect.w = join.player_name_box.w - (BOARDER_THICKNESS<<1);
join.player_name_rect.h = join.player_name_box.h - (BOARDER_THICKNESS<<1);
join.lobby_name_box.x = join.player_name_box.x;
join.lobby_name_box.y = join.player_name_box.y + join.player_name_box.h + BOARDER_THICKNESS;
join.lobby_name_box.w = join.player_name_box.w;
join.lobby_name_box.h = join.player_name_box.h;
join.lobby_name_rect.x = join.player_name_rect.x;
join.lobby_name_rect.y = join.lobby_name_box.y + BOARDER_THICKNESS;
join.lobby_name_rect.w = join.player_name_rect.w;
join.lobby_name_rect.h = join.player_name_rect.h;
join.join_game_box.w = join.lobby_name_box.w >> 1;
join.join_game_box.x = (DEF_WINDOW_WIDTH >> 1) - (join.join_game_box.w >> 1);
join.join_game_box.y = join.lobby_name_box.y + join.lobby_name_box.h + BOARDER_THICKNESS;
join.join_game_box.h = join.lobby_name_box.h;
join.join_game_rect.x = join.join_game_box.x + BOARDER_THICKNESS;
join.join_game_rect.y = join.join_game_box.y + BOARDER_THICKNESS;
join.join_game_rect.w = join.join_game_box.w - (BOARDER_THICKNESS << 1);
join.join_game_rect.h = join.join_game_box.h - (BOARDER_THICKNESS << 1);
join.join_surf = TTF_RenderText_Solid(join.font, "JOIN GAME", white);
join.join_tex = SDL_CreateTextureFromSurface(renderer, join.join_surf);
PRINT_M(INIT JOIN)
return;
}
void join_update_hover()
{
if(SDL_PointInRect(&mouse_pos, &join.join_game_box)){ join.over_object = JOIN_GAME_BUTTON; }
else if(SDL_PointInRect(&mouse_pos, &join.lobby_name_box)){ join.over_object = LOBBY_NAME_BOX; }
else if(SDL_PointInRect(&mouse_pos, &join.player_name_box)){ join.over_object = PLAYER_NAME_BOX; }
else { join.over_object = NO_OBJECT; }
return;
}
void render_join_game()
{
join_update_hover();
join.player_name_surf = TTF_RenderText_Solid(join.font, join.player_name, white);
join.player_name_tex = SDL_CreateTextureFromSurface(renderer, join.player_name_surf);
join.lobby_name_surf = TTF_RenderText_Solid(join.font, join.lobby_name, white);
join.lobby_name_tex = SDL_CreateTextureFromSurface(renderer, join.lobby_name_surf);
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
SDL_RenderClear(renderer);
if(join.selected == PLAYER_NAME_SEL ){
//player name rect
SDL_SetRenderDrawColor(renderer, 32, 64, 32, 255);
SDL_RenderFillRect(renderer, &join.player_name_box);
//lobby name rect
SDL_SetRenderDrawColor(renderer, 64,64,64,255);
SDL_RenderFillRect(renderer, &join.lobby_name_box);
}
else{
//lobby name rect
SDL_SetRenderDrawColor(renderer, 32, 64, 32, 255);
SDL_RenderFillRect(renderer, &join.lobby_name_box);
//player name rect
SDL_SetRenderDrawColor(renderer, 64,64,64,255);
SDL_RenderFillRect(renderer, &join.player_name_box);
}
//player name text
join.player_name_rect.w = (join.player_name_box.w / MAX_NAMESZ) * strlen(join.player_name);
SDL_RenderCopy(renderer, join.player_name_tex, NULL, &join.player_name_rect);
//lobby name text
join.lobby_name_rect.w = (join.lobby_name_box.w / MAX_NAMESZ) * strlen(join.lobby_name);
SDL_RenderCopy(renderer, join.lobby_name_tex, NULL, &join.lobby_name_rect);
SDL_RenderFillRect(renderer, &join.join_game_box);
SDL_RenderCopy(renderer, join.join_tex, NULL, &join.join_game_rect);
SDL_RenderPresent(renderer);
SDL_FreeSurface(join.player_name_surf);
SDL_DestroyTexture(join.player_name_tex);
SDL_FreeSurface(join.lobby_name_surf);
SDL_DestroyTexture(join.lobby_name_tex);
return;
}

211
main.c
View File

@@ -1,74 +1,85 @@
/*Troy Rosin*/
#include <main.h>
#include <game.h>
#include <input.h>
#include <overworld.h>
#include <host.h>
#include <options.h>
#include <mydebug.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>
#define SPACING 20
void render_mainmenu();
void init_mainmenu();
SDL_Point mouse_pos;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;
SDL_Color white = {255,255,255,0};
struct mainmenu_ctx {
TTF_Font* font;
SDL_Texture* background;
SDL_Surface *start_surface, *host_surface, *join_surface, *exit_surface, *options_surface;
SDL_Texture *start_text, *host_text, *join_text, *exit_text, *options_text;
SDL_Rect start_rect, host_rect, join_rect, exit_rect, options_rect;
SDL_Rect mainmenu_rects[MAIN_SELECTIONS];
};
struct mainmenu_ctx mainmenu;
int state = TITLE;
int running;
int selected_index = START_GAME;
int main(void) {
int main(void)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Shit Game",\
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DEF_WINDOW_WIDTH,
DEF_WINDOW_HEIGHT, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
IMG_Init(IMG_INIT_PNG);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
TTF_Init();
SDL_CaptureMouse(SDL_TRUE);
SDL_Texture* background = IMG_LoadTexture(renderer, "assets/mainscreen.png");
SDL_Texture* start_img = IMG_LoadTexture(renderer, "assets/mainscreen_start.png");
SDL_Texture* exit_img = IMG_LoadTexture(renderer, "assets/mainscreen_exit.png");
SDL_Texture* selection_img = start_img;
if (!background || !start_img || !exit_img) {
printf("IMG_LoadTexture error: %s\n", IMG_GetError());
}
int ret = 0;
ret = SDL_SetTextureColorMod(start_img, 255, 0, 0);
ret |= SDL_SetTextureColorMod(exit_img, 255, 0, 0);
if(ret){
puts("SetTextureColorMod error");
}
init_mainmenu();
init_start_game();
init_join();
init_host();
init_overworld();
init_options();
running = 1;
//main game loop
while (running)
{
handle_event(&event);
SDL_RenderClear(renderer);
SDL_GetMouseState(&mouse_pos.x, &mouse_pos.y);
if(selected_index == START_GAME){ selection_img = start_img;}
else if(selected_index == EXIT_GAME) {selection_img = exit_img;}
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, selection_img, NULL, NULL);
SDL_RenderPresent(renderer);
if(state)
switch(state)
{
switch(state)
{
case BATTLE:
start_battle();
break;
}
case START:
render_start_game();
break;
case OVERWORLD:
render_overworld();
break;
case HOST:
render_host_game();
break;
case JOIN:
render_join_game();
break;
case OPTIONS:
render_options();
break;
case TITLE:
render_mainmenu();
break;
}
}
SDL_DestroyRenderer(renderer);
@@ -78,3 +89,119 @@ DEF_WINDOW_HEIGHT, 0);
return 0;
}
void render_mainmenu_selected()
{
int x1, x2, y1, y2;
switch(selected_index)
{
case START_GAME:
x1 = mainmenu.start_rect.x;
x2 = x1 + mainmenu.start_rect.w;
y1 = y2 = mainmenu.start_rect.y + mainmenu.start_rect.h;
break;
case HOST_GAME:
x1 = mainmenu.host_rect.x;
x2 = x1 + mainmenu.host_rect.w;
y1 = y2 = mainmenu.host_rect.y + mainmenu.host_rect.h;
break;
case JOIN_GAME:
x1 = mainmenu.join_rect.x;
x2 = x1 + mainmenu.join_rect.w;
y1 = y2 = mainmenu.join_rect.y + mainmenu.join_rect.h;
break;
case OPTIONS_MENU:
x1 = mainmenu.options_rect.x;
x2 = x1 + mainmenu.options_rect.w;
y1 = y2 = mainmenu.options_rect.y + mainmenu.options_rect.h;
break;
case EXIT_GAME:
x1 = mainmenu.exit_rect.x;
x2 = x1 + mainmenu.exit_rect.w;
y1 = y2 = mainmenu.exit_rect.y + mainmenu.exit_rect.h;
break;
}
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
void init_mainmenu()
{
window = SDL_CreateWindow("Shufflers", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, DEF_WINDOW_WIDTH,DEF_WINDOW_HEIGHT, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
IMG_Init(IMG_INIT_PNG);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_CaptureMouse(SDL_TRUE);
SDL_StartTextInput();
mainmenu.background = IMG_LoadTexture(renderer, "assets/mainscreen.png");
mainmenu.font = TTF_OpenFont("assets/font.ttf", 24);
mainmenu.start_surface = TTF_RenderText_Solid(mainmenu.font, "START", white);
mainmenu.start_text = SDL_CreateTextureFromSurface(renderer, mainmenu.start_surface);
mainmenu.host_surface = TTF_RenderText_Solid(mainmenu.font, "HOST", white);
mainmenu.host_text = SDL_CreateTextureFromSurface(renderer, mainmenu.host_surface);
mainmenu.join_surface = TTF_RenderText_Solid(mainmenu.font, "JOIN", white);
mainmenu.join_text = SDL_CreateTextureFromSurface(renderer, mainmenu.join_surface);
mainmenu.exit_surface = TTF_RenderText_Solid(mainmenu.font, "EXIT", white);
mainmenu.exit_text = SDL_CreateTextureFromSurface(renderer, mainmenu.exit_surface);
mainmenu.options_surface = TTF_RenderText_Solid(mainmenu.font, "OPTIONS", white);
mainmenu.options_text = SDL_CreateTextureFromSurface(renderer, mainmenu.options_surface);
mainmenu.start_rect.w = (DEF_WINDOW_WIDTH >> 2) - (DEF_WINDOW_WIDTH >> 3);
mainmenu.start_rect.h = (DEF_WINDOW_HEIGHT >> 3) - (DEF_WINDOW_HEIGHT >> 4);
mainmenu.start_rect.x = (DEF_WINDOW_WIDTH - mainmenu.start_rect.w) >> 1;
mainmenu.start_rect.y = (DEF_WINDOW_HEIGHT - mainmenu.start_rect.h) >> 1;
mainmenu.host_rect.w = mainmenu.join_rect.w = mainmenu.exit_rect.w =
mainmenu.options_rect.w = mainmenu.start_rect.w;
mainmenu.options_rect.w <<= 1;
mainmenu.host_rect.h = mainmenu.join_rect.h = mainmenu.exit_rect.h =
mainmenu.options_rect.h = mainmenu.start_rect.h;
mainmenu.host_rect.x = mainmenu.join_rect.x = mainmenu.exit_rect.x =
mainmenu.options_rect.x = mainmenu.start_rect.x;
mainmenu.options_rect.x -= mainmenu.join_rect.w >> 1;
mainmenu.host_rect.y = mainmenu.start_rect.y + mainmenu.start_rect.h + SPACING;
mainmenu.join_rect.y = mainmenu.host_rect.y + mainmenu.host_rect.h + SPACING;
mainmenu.options_rect.y = mainmenu.join_rect.y + mainmenu.join_rect.h + SPACING;
mainmenu.exit_rect.y = mainmenu.options_rect.y + mainmenu.options_rect.h + SPACING;
mainmenu.mainmenu_rects[START_GAME] = mainmenu.start_rect;
mainmenu.mainmenu_rects[HOST_GAME] = mainmenu.host_rect;
mainmenu.mainmenu_rects[JOIN_GAME] = mainmenu.join_rect;
mainmenu.mainmenu_rects[OPTIONS_MENU] = mainmenu.options_rect;
mainmenu.mainmenu_rects[EXIT_GAME] = mainmenu.exit_rect;
return;
}
void render_mainmenu()
{
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0,196,0,255);
SDL_RenderCopy(renderer, mainmenu.background, NULL, NULL);
SDL_RenderCopy(renderer, mainmenu.start_text, NULL, &(mainmenu.start_rect));
SDL_RenderCopy(renderer, mainmenu.host_text, NULL, &mainmenu.host_rect);
SDL_RenderCopy(renderer, mainmenu.join_text, NULL, &mainmenu.join_rect);
SDL_RenderCopy(renderer, mainmenu.options_text, NULL, &mainmenu.options_rect);
SDL_RenderCopy(renderer, mainmenu.exit_text, NULL, &mainmenu.exit_rect);
for(int i = 0; i<MAIN_SELECTIONS; i++)
{
if(SDL_PointInRect(&mouse_pos, &mainmenu.mainmenu_rects[i]))
{
selected_index = i;
break;
}
}
render_mainmenu_selected();
SDL_RenderPresent(renderer);
return;
}

105
net.c Normal file
View File

@@ -0,0 +1,105 @@
/*Troy Rosin*/
#include <SDL_net.h>
#include <SDLnetsys.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.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;
}
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;
}

68
net_proto.txt Normal file
View File

@@ -0,0 +1,68 @@
we will use this port in net.h
#define PORT 36911
here packet size is also defined, subject to change but makes it
easy to no block on send
#define PACKSZ 1024
pack type will be at the front of the packet to ensure we handle packets right
enum pack_type{
G_STATE, S_STATE, HOSTS_REQ, NEW_HOST, JOIN_REQ, U_NAME
};
packets are defined so that they keep the exact size of PACKSZ
typedef struct packet{
enum pack_type type;
char payload[PACKSZ - sizeof(enum pack_type)];
} packet;
JOIN GAME:
CLIENT | SERVER
HOSTS_REQ ----------->
<---------- HOSTS_REQ [ list of names that are open lobbies]
------------------------------------------------------------------------------------------
JOIN_REQ ----------->
[name of lobby, name of player]
<----------- JOIN_REQ [IP info of host with that name]
HOST GAME:
CLIENT | SERVER
NEW_HOST ------------>
[name of lobby]
<----------- NEW_HOST [ack]
(ON PLAYER JOIN REQUEST)
<------------ JOIN_REQ [player ip info]
JOIN_REQ ------------>
[lobby count]
(ON HOST START GAME)
S_STATE ----------->
[starting game]
<---------- S_STATE [ack]
(on timeout for lobbies ie games not started)
<--------- G_STATE [you alive?]
G_STATE --------->
[yup]

130
options.c Normal file
View File

@@ -0,0 +1,130 @@
/*Troy Rosin*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <options.h>
#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;
}

80
overworld.c Normal file
View File

@@ -0,0 +1,80 @@
/*Troy Rosin*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <overworld.h>
#include <err_check.h>
#define OW_ROWS ((DEF_WINDOW_WIDTH / OW_GRIDSZ) + 1)
#define OW_COLS ((DEF_WINDOW_HEIGHT / OW_GRIDSZ) + 1)
PLAYER *myplayer, me;
SDL_Texture *selected_grid, *player_sprite, *grass;
PLAYER *get_this_player()
{
return &me;
}
SDL_Rect ow_rects[OW_ROWS][OW_COLS];
void init_overworld()
{
int row, col;
PRINT_M(INIT OVERWORLD)
IMG_GETANDCHECK(grass, renderer, "assets/ow_grid_grass.png")
IMG_GETANDCHECK(selected_grid, renderer, "assets/ow_selected_grid.png");
IMG_GETANDCHECK(player_sprite, renderer, "assets/player.png");
myplayer = get_this_player();
//set up rects to render textures into
for(row = 0; row <= OW_ROWS; row++)
{
for(col = 0; col <= OW_COLS; col++)
{
ow_rects[row][col].x = row * OW_GRIDSZ;
ow_rects[row][col].y = col * OW_GRIDSZ;
ow_rects[row][col].h = OW_GRIDSZ;
ow_rects[row][col].w = OW_GRIDSZ;
}
}
myplayer->texture = player_sprite;
myplayer->pos.x = OW_ROWS >> 1;
myplayer->pos.y = OW_COLS >> 1;
return;
}
void render_overworld()
{
int row, col;
SDL_RenderClear(renderer);
//render the ow grid
for(row = 0; row < OW_ROWS; row++)
{
for(col = 0; col < OW_COLS; col++)
{
SDL_RenderCopy(renderer, grass, NULL, &(ow_rects[row][col]));
}
}
SDL_RenderCopy(renderer, myplayer->texture, NULL, &(ow_rects[myplayer->pos.x][myplayer->pos.y]));
SDL_RenderPresent(renderer);
return;
}

159
server.c Normal file
View File

@@ -0,0 +1,159 @@
//troy Rosin
#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>
#define MAX_SOCKS 4
char buff[PACKSZ];
char pack[PACKSZ], pack2[PACKSZ];
void game_server()
{
IPaddress *ip, _ip, *cip;
TCPsocket sock, csock;
TCPsocket sock_arr[MAX_SOCKS];
ip = &_ip;
SDLNet_SocketSet socks;
int i;
Uint16 c_port;
Uint32 c_ip;
int flag = 1;
int ret = 0;
SDL_Init(0);
SDLNet_Init();
if(SDLNet_ResolveHost(ip, NULL, PORT) == -1)
{
printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
exit(1);
}
sock = SDLNet_TCP_Open(ip);
if(!sock)
{
printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
exit(1);
}
socks = SDLNet_AllocSocketSet(MAX_SOCKS);
for(i = 0; i < MAX_SOCKS; ++i)
{
sock_arr[i] = NULL;
}
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)
{//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);
if(ret < PACKSZ){
printf("TCP recv error: %s\n", SDLNet_GetError());
sock_arr[i] = NULL;
SDLNet_TCP_DelSocket(socks, csock);
SDLNet_TCP_Close(csock);
continue;
}
read_to_buff((Uint32*)buff, (Uint32*)pack, PACKSZ);
switch(((packet*)buff)->type)
{
case G_STATE:
PRINT_M("got to get state case");
break;
case S_STATE:
PRINT_M("got to set state case");
break;
case HOSTS_REQ:
PRINT_M("got to hosts request state");
break;
case NEW_HOST:
PRINT_M("got to new host state");
break;
case JOIN_REQ:
PRINT_M("got to join request state");
break;
case U_NAME:
PRINT_M("got to update name state");
break;
}
}
}
}
else if (csock) {
cip = SDLNet_TCP_GetPeerAddress(csock);
if(!cip){
printf("get peer addr error: %s\n", SDLNet_GetError());
}
else {
PRINT_M("we got a new connection!");
if(SDLNet_TCP_AddSocket(socks, csock) <= 0){
printf("add sock to set error: %s\n", SDLNet_GetError());
}
for(i = 0; i < MAX_SOCKS; ++i){
if(sock_arr[i] == NULL){ sock_arr[i] = csock; break; }
}
}
}
}
SDLNet_TCP_Close(sock);
SDLNet_Quit();
SDL_Quit();
}
int main(void)
{
game_server();
puts("donzo");
return 0;
}
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;
}