using git now lol

This commit is contained in:
2026-01-21 13:51:24 -06:00
parent 3117428afe
commit f3c0aafe2f
20 changed files with 529 additions and 0 deletions

BIN
assets/battle_grid.ase Normal file

Binary file not shown.

BIN
assets/battle_grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
assets/default_card.ase Normal file

Binary file not shown.

BIN
assets/default_card.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 651 B

BIN
assets/mainscreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
assets/mainscreen_exit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
assets/mainscreen_start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
assets/maroon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
assets/selected_card.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 469 B

BIN
assets/selected_grid.ase Normal file

Binary file not shown.

BIN
assets/selected_grid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 237 B

BIN
assets/sprites/player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

201
battle.c Normal file
View File

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

9
game.c Normal file
View File

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

22
include/deck.h Normal file
View File

@@ -0,0 +1,22 @@
//Troy Rosin
#ifndef __deck_h__
#define __deck_h__
#include <SDL2/SDL.h>
#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

37
include/game.h Normal file
View File

@@ -0,0 +1,37 @@
//Troy Rosin
#ifndef __game_h__
#define __game_h__
#include <SDL2/SDL.h>
#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

12
include/input.h Normal file
View File

@@ -0,0 +1,12 @@
/*Troy Rosin*/
#ifndef __input_h__
#define __input_h__
#include <SDL2/SDL.h>
void handle_input(SDL_Event *e);
void handle_event(SDL_Event *e);
#endif

89
include/mydebug.h Normal file
View File

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

79
input.c Normal file
View File

@@ -0,0 +1,79 @@
/*Troy Rosin*/
#include <SDL2/SDL.h>
#include <game.h>
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;
}

80
main.c Normal file
View File

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