diff --git a/assets/battle_grid.ase b/assets/battle_grid.ase new file mode 100644 index 0000000..741636e Binary files /dev/null and b/assets/battle_grid.ase differ diff --git a/assets/battle_grid.png b/assets/battle_grid.png new file mode 100644 index 0000000..3aebeda Binary files /dev/null and b/assets/battle_grid.png differ diff --git a/assets/default_card.ase b/assets/default_card.ase new file mode 100644 index 0000000..683818f Binary files /dev/null and b/assets/default_card.ase differ diff --git a/assets/default_card.png b/assets/default_card.png new file mode 100644 index 0000000..acfefe5 Binary files /dev/null and b/assets/default_card.png differ diff --git a/assets/mainscreen.png b/assets/mainscreen.png new file mode 100644 index 0000000..d6d92a2 Binary files /dev/null and b/assets/mainscreen.png differ diff --git a/assets/mainscreen_exit.png b/assets/mainscreen_exit.png new file mode 100644 index 0000000..03bb5d4 Binary files /dev/null and b/assets/mainscreen_exit.png differ diff --git a/assets/mainscreen_start.png b/assets/mainscreen_start.png new file mode 100644 index 0000000..74cda66 Binary files /dev/null and b/assets/mainscreen_start.png differ diff --git a/assets/maroon.png b/assets/maroon.png new file mode 100644 index 0000000..bef64f1 Binary files /dev/null and b/assets/maroon.png differ diff --git a/assets/selected_card.png b/assets/selected_card.png new file mode 100644 index 0000000..aa88bb9 Binary files /dev/null and b/assets/selected_card.png differ diff --git a/assets/selected_grid.ase b/assets/selected_grid.ase new file mode 100644 index 0000000..2f8d010 Binary files /dev/null and b/assets/selected_grid.ase differ diff --git a/assets/selected_grid.png b/assets/selected_grid.png new file mode 100644 index 0000000..7612a6a Binary files /dev/null and b/assets/selected_grid.png differ diff --git a/assets/sprites/player.png b/assets/sprites/player.png new file mode 100644 index 0000000..5627ace Binary files /dev/null and b/assets/sprites/player.png differ diff --git a/battle.c b/battle.c new file mode 100644 index 0000000..fd6f20b --- /dev/null +++ b/battle.c @@ -0,0 +1,201 @@ +/*Troy Rosin*/ + +#include +#include +#include +#include +#include +#include + +#define BOARDER_THICKNESS 10 +//do not let dx or dy be anything less than 2 +#define DX 5 +#define DY 5 +#define DRAW_TIMEOUT 10 + +SDL_Rect mouse_rect; +SDL_Rect bot_rect, hand_rect; +SDL_Point mouse_pos; + +int selected_card; + +int dragging; + +void start_battle(){ + int cardoff_x, cardoff_y; + int mx, my; + 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"); + + mouse_rect.x = 0; + mouse_rect.y = 0; + mouse_rect.w = 5; + mouse_rect.h = 5; + + bot_rect.w = DEF_WINDOW_WIDTH - 2 * BOARDER_THICKNESS; + bot_rect.x = BOARDER_THICKNESS; + bot_rect.h = DEF_WINDOW_HEIGHT >> 3; + bot_rect.y = DEF_WINDOW_HEIGHT - BOARDER_THICKNESS - bot_rect.h; + + hand_rect.w = bot_rect.w >> 1; + hand_rect.x = DEF_WINDOW_WIDTH >> 2; + hand_rect.y = bot_rect.y - BOARDER_THICKNESS; + hand_rect.h = bot_rect.h; + + + + mouse_pos.x = mouse_pos.y = 0; + + if(!grid || !card || !selected) + { + printf("IMG_LOAD err: %s\n", IMG_GetError()); + } + + Uint64 lastdraw = SDL_GetTicks64() + DRAW_TIMEOUT; + + HAND hand; + hand.handsize = 7; + for(int i=0; i<7; i++){ + hand.cards[i] = card; + hand.card_rects[i].w = CARD_SIZE; + hand.card_rects[i].h = CARD_SIZE; + } + + while(running) + { + handle_event(&event); + + 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) + { + lastdraw = SDL_GetTicks() + DRAW_TIMEOUT; + if(inside) + { + if(hand_rect.w < (DEF_WINDOW_WIDTH >> 1) + (DEF_WINDOW_WIDTH >> 3) ) + { //growing + hand_rect.x -= DX>>1; + hand_rect.w += DX; + hand_rect.y -= DY; + hand_rect.h += DY; + } + }else if(hand_rect.w > bot_rect.w >> 1){ + //shrinking + hand_rect.x += DX>>1; + hand_rect.w -= DX; + hand_rect.y += DY; + hand_rect.h -= DY; + if(!dragging){ selected_card = -1; } + } + } + int useable_hand_space = hand_rect.w - (BOARDER_THICKNESS << 1) - CARD_W; + int spacing; + if(hand.handsize == 1) + { + spacing = useable_hand_space; + }else + { + spacing = useable_hand_space / (hand.handsize - 1); + } + int 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++) + { + hand.card_rects[i].x = hand_rect.x + BOARDER_THICKNESS + ( spacing * i) + - ((CARD_SIZE - CARD_W)>>1) ; + hand.card_rects[i].y = hand_rect.y + BOARDER_THICKNESS; + if(check && SDL_PointInRect(&mouse_pos, &(hand.card_rects[i]))) + { + selected_card = i; + } + } + + //handle dragging cards + if(dragging) + { + if(dragging == GRAB_OLD) + { + hand.card_rects[selected_card].x = mouse_pos.x - cardoff_x; + hand.card_rects[selected_card].y = mouse_pos.y - cardoff_y; + } + else if(dragging == GRAB_NEW) + { + if(selected_card != -1) + { + cardoff_x = mouse_pos.x - hand.card_rects[selected_card].x; + cardoff_y = mouse_pos.y - hand.card_rects[selected_card].y; + dragging = GRAB_OLD; + } else + { + dragging = NOT_DRAG; + } + } + else if(dragging == LETGO && selected_card != -1){ + dragging = NOT_DRAG; + if(!inside) + { + hand.handsize--; + for(int i = selected_card; i < hand.handsize; i++) + { + hand.cards[i] = hand.cards[i+1]; + } + selected_card = -1; + hand.cards[hand.handsize + 1] = NULL; + } + } + } + SDL_RenderClear(renderer); + //background + SDL_RenderCopy(renderer, grid, NULL, NULL); + + //bottom box for ui shit + SDL_SetRenderDrawColor(renderer, 0,0,0,255); + SDL_RenderFillRect(renderer, &bot_rect); + + //box for hand of cards + SDL_SetRenderDrawColor(renderer, 64,64,64,255); + SDL_RenderFillRect(renderer, &hand_rect); + + //mouse follower box + SDL_SetRenderDrawColor(renderer, 0,255,0,255); + SDL_RenderFillRect(renderer, &mouse_rect); + + //render cards + for(int i = 0; i < hand.handsize; i++) + { + if(selected_card != i) + { + SDL_RenderCopy(renderer, hand.cards[i], NULL, &(hand.card_rects[i])); + } + } + //render selected card last + if(selected_card != -1) + { + if(!dragging){ hand.card_rects[selected_card].y -= BOARDER_THICKNESS; } + SDL_RenderCopy(renderer, hand.cards[selected_card], NULL, + &(hand.card_rects[selected_card])); + SDL_RenderCopy(renderer, selected, NULL, + &(hand.card_rects[selected_card])); + } + + SDL_RenderPresent(renderer); + } + + return; +} + + + + diff --git a/game.c b/game.c new file mode 100644 index 0000000..16a9cd4 --- /dev/null +++ b/game.c @@ -0,0 +1,9 @@ +/*Troy Rosin*/ + +#include +#include +#include +#include +#include + + diff --git a/include/deck.h b/include/deck.h new file mode 100644 index 0000000..0704b48 --- /dev/null +++ b/include/deck.h @@ -0,0 +1,22 @@ +//Troy Rosin + +#ifndef __deck_h__ +#define __deck_h__ + +#include + +#define MAX_HANDSIZE 8 +#define CARD_SIZE (128 * 2) +#define CARD_W (96 * 2) + +typedef struct hand +{ + int handsize; + SDL_Texture *cards[MAX_HANDSIZE]; + SDL_Rect card_rects[MAX_HANDSIZE]; +} HAND; + + + + +#endif diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..57d66a4 --- /dev/null +++ b/include/game.h @@ -0,0 +1,37 @@ +//Troy Rosin + +#ifndef __game_h__ +#define __game_h__ + +#include + + +#define DEF_WINDOW_WIDTH 1920 +#define DEF_WINDOW_HEIGHT 1080 + + +extern int running; +extern int state; + +#define TITLE 0 +#define BATTLE 1 +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; + +#endif diff --git a/include/input.h b/include/input.h new file mode 100644 index 0000000..b4e5ebc --- /dev/null +++ b/include/input.h @@ -0,0 +1,12 @@ +/*Troy Rosin*/ + +#ifndef __input_h__ +#define __input_h__ + +#include + +void handle_input(SDL_Event *e); + +void handle_event(SDL_Event *e); + +#endif diff --git a/include/mydebug.h b/include/mydebug.h new file mode 100644 index 0000000..dd62116 --- /dev/null +++ b/include/mydebug.h @@ -0,0 +1,89 @@ +#ifndef __mydebug_h__ +#define __mydebug_h__ + +/*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 + +#endif diff --git a/input.c b/input.c new file mode 100644 index 0000000..d2df6bc --- /dev/null +++ b/input.c @@ -0,0 +1,79 @@ +/*Troy Rosin*/ + +#include +#include + +extern int running; + +void handle_input_mainmenu(SDL_Event *e); +void handle_input_battle(SDL_Event *e); + + +void handle_event(SDL_Event *e){ + while(SDL_PollEvent(e)) + { + switch(state) + { + case TITLE: + if(e->type == SDL_KEYDOWN) + { + handle_input_mainmenu(e); + } + break; + case BATTLE: + switch (e->type) + { + case SDL_KEYDOWN: + handle_input_battle(e); + break; + case SDL_MOUSEBUTTONDOWN: + dragging = GRAB_NEW; + break; + case SDL_MOUSEBUTTONUP: + dragging = LETGO; + } + break; + } + return; + } +} + +void handle_input_mainmenu(SDL_Event *e) +{ + switch(e->key.keysym.sym){ + case SDLK_ESCAPE: + running = 0; + break; + case SDLK_w: + if (selected_index) { selected_index--; } + else { selected_index = MAIN_SELECTIONS - 1; } + selected_index %= MAIN_SELECTIONS; + break; + case SDLK_s: + selected_index++; + selected_index %= MAIN_SELECTIONS; + break; + case SDLK_RETURN: + if(selected_index == EXIT_GAME){ + running = 0; + } + else{ + state = BATTLE; + } + } + return; +} + + +void handle_input_battle(SDL_Event *e) +{ + switch(e->key.keysym.sym) + { + case SDLK_ESCAPE: + running = 0; + break; + default: + printf("%d", (char)e->key.keysym.sym); + } + return; +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..38d49fc --- /dev/null +++ b/main.c @@ -0,0 +1,80 @@ +/*Troy Rosin*/ + +#include +#include + +#include +#include +#include + +SDL_Window* window; +SDL_Renderer* renderer; +SDL_Event event; + +int state = TITLE; +int running; + + +int selected_index = START_GAME; + +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); + + 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"); + } + + running = 1; + + while (running) + { + handle_event(&event); + + SDL_RenderClear(renderer); + + 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) + { + case BATTLE: + start_battle(); + break; + } + } + + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + IMG_Quit(); + SDL_Quit(); + return 0; +} +