202 lines
5.1 KiB
C
202 lines
5.1 KiB
C
/*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;
|
|
}
|
|
|
|
|
|
|
|
|