Skip to content

Commit

Permalink
Added custom cursor, reworked battle menu
Browse files Browse the repository at this point in the history
  • Loading branch information
123outerme committed Oct 11, 2021
1 parent 3e69f66 commit 331b6c1
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 79 deletions.
Binary file modified assets/uiTilesheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
251 changes: 209 additions & 42 deletions main.c

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions mapMaker.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool createNewMap(warperTilemap* tilemap, int tileSize)
cResource loadChoiceBoxRes;
createMenuTextBox(&loadChoiceBox, (cDoubleRect) {tileSize, tileSize, global.windowW - 2 * tileSize, global.windowH - 2 * tileSize}, (cDoublePt) {412, 8}, 4, true, 0xFF, (char*[4]) {"Load Map?", " ", "Yes", "No"}, (bool[4]) {false, false, true, true}, 4, &global.mainFont);
initCResource(&loadChoiceBoxRes, (void*) &loadChoiceBox, drawWarperTextBox, destroyWarperTextBox, 5);
initCScene(&inputScene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &inputCamera, NULL, 0, NULL, 0, (cResource*[1]) {&loadChoiceBoxRes}, 1, NULL, 0);
initCScene(&inputScene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &inputCamera, (cSprite*[1]) {&cursorSprite}, 1, NULL, 0, (cResource*[1]) {&loadChoiceBoxRes}, 1, NULL, 0);

loadChoiceBox.selection = -1;

Expand All @@ -30,8 +30,11 @@ bool createNewMap(warperTilemap* tilemap, int tileSize)
if (input.isClick)
{
if (loadChoiceBoxRes.renderLayer != 0)
checkWarperTextBoxClick(&loadChoiceBox, input.click.x, input.click.y);
checkWarperTextBoxSelection(&loadChoiceBox, input.click.x, input.click.y);
}

updateCursorPos(input.motion, false);

drawCScene(&inputScene, true, true, NULL, NULL, 60);
}
loadMap = (loadChoiceBox.selection == 2);
Expand Down Expand Up @@ -159,34 +162,33 @@ bool createNewMap(warperTilemap* tilemap, int tileSize)
initCSprite(&collisionSprites[x * tilemap->height + y], tilesetTexture, "./assets/worldTilesheet.png", tilemap->collisionmap[x][y],
(cDoubleRect) {tilemap->tileSize * x, tilemap->tileSize * y, tilemap->tileSize, tilemap->tileSize},
(cDoubleRect) {39 * tilemap->tileSize / 2, (tilemap->collisionmap[x][y] + 18) * tilemap->tileSize / 2, tilemap->tileSize / 2, tilemap->tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 3);
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 3);
}
}
initC2DModel(&collisionModel, collisionSprites, tilemap->width * tilemap->height, (cDoublePt) {0, 0}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 0);

initCSprite(&leftTileSprite, tilesetTexture, "./assets/worldTilesheet.png", 0,
(cDoubleRect) {tilemap->tileSize, tilemap->tileSize, tilemap->tileSize, tilemap->tileSize},
(cDoubleRect) {0, 0, tilemap->tileSize / 2, tilemap->tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0, false, NULL, 2);
NULL, 1.0, SDL_FLIP_NONE, 0, false, false, NULL, 2);

initCSprite(&rightTileSprite, tilesetTexture, "./assets/worldTilesheet.png", 0,
(cDoubleRect) {tilemap->tileSize, tilemap->tileSize, tilemap->tileSize, tilemap->tileSize},
(cDoubleRect) {0, 0, tilemap->tileSize / 2, tilemap->tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0, false, NULL, 0);
NULL, 1.0, SDL_FLIP_NONE, 0, false, false, NULL, 0);

initCSprite(&tilesetSprite, tilesetTexture, "./assets/worldTileset.png", 0,
(cDoubleRect) {0, 0, 640, 320},
(cDoubleRect) {0, 0, 640, 320},
NULL, 2.0, SDL_FLIP_NONE, 0, true, NULL, 0);
NULL, 2.0, SDL_FLIP_NONE, 0, true, false, NULL, 0);
}

cResource shadeResource;
warperFilter filter = initWarperFilter(0x00, 0x00, 0x00, 0x58);
initCResource(&shadeResource, &filter, drawWarperFilter, destroyWarperFilter, 0);

initCCamera(&inputCamera, (cDoubleRect) {0, 0, global.windowW, global.windowH}, 1.0, 0);
initCScene(&inputScene, (SDL_Color) {0xFF, 0xFF, 0xFF}, &inputCamera, (cSprite*[3]) {&tilesetSprite, &leftTileSprite, &rightTileSprite}, 3, (c2DModel*[3]) {&mapModel_layer1, &mapModel_layer2, &collisionModel}, 3, (cResource*[1]) {&shadeResource}, 1, NULL, 0);

initCScene(&inputScene, (SDL_Color) {0xFF, 0xFF, 0xFF}, &inputCamera, (cSprite*[4]) {&cursorSprite, &tilesetSprite, &leftTileSprite, &rightTileSprite}, 4, (c2DModel*[3]) {&mapModel_layer1, &mapModel_layer2, &collisionModel}, 3, (cResource*[1]) {&shadeResource}, 1, NULL, 0);

int previousIndex = 0;

Expand Down Expand Up @@ -467,6 +469,8 @@ bool createNewMap(warperTilemap* tilemap, int tileSize)
}
if (e.type == SDL_MOUSEMOTION)
{
updateCursorPos(e.motion, false);

leftTileSprite.drawRect.x = e.motion.x + inputCamera.rect.x - tilemap->tileSize / 2;
leftTileSprite.drawRect.y = e.motion.y + inputCamera.rect.y - tilemap->tileSize / 2;

Expand Down
4 changes: 2 additions & 2 deletions warper.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ void loadTilemapModels(warperTilemap tilemap, c2DModel* layer1, c2DModel* layer2
initCSprite(&tileSprites_layer1[x * tilemap.height + y], tilesetTexture, "assets/worldTilesheet.png", tilemap.spritemap_layer1[x][y],
(cDoubleRect) {tilemap.tileSize * x, tilemap.tileSize * y, tilemap.tileSize, tilemap.tileSize},
(cDoubleRect) {(tilemap.spritemap_layer1[x][y] / 20) * tilemap.tileSize / 2, (tilemap.spritemap_layer1[x][y] % 20) * tilemap.tileSize / 2, tilemap.tileSize / 2, tilemap.tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 5);
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 5);

initCSprite(&tileSprites_layer2[x * tilemap.height + y], tilesetTexture, "assets/worldTilesheet.png", tilemap.spritemap_layer2[x][y],
(cDoubleRect) {tilemap.tileSize * x, tilemap.tileSize * y, tilemap.tileSize, tilemap.tileSize},
(cDoubleRect) {(tilemap.spritemap_layer2[x][y] / 20) * tilemap.tileSize / 2, (tilemap.spritemap_layer2[x][y] % 20) * tilemap.tileSize / 2, tilemap.tileSize / 2, tilemap.tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 5);
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 5);
}
}
initC2DModel(layer1, tileSprites_layer1, tilemap.width * tilemap.height, (cDoublePt) {0, 0}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 5);
Expand Down
3 changes: 3 additions & 0 deletions warper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#define GRID_MAX_OPACITY 0x40
#define WARPER_OPTIONS_FILE "assets/options.cfg"

#define SCREEN_PX_WIDTH 40 * TILE_SIZE
#define SCREEN_PX_HEIGHT 20 * TILE_SIZE

typedef struct _warperTilemap
{
int** spritemap_layer1;
Expand Down
105 changes: 79 additions & 26 deletions warperInterface.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "warperInterface.h"

cSprite cursorSprite;
cResource cursorResource;

/** \brief
*
* \param textBox warperTextBox*
Expand Down Expand Up @@ -85,30 +88,6 @@ void drawWarperTextBox(void* textBoxSubclass, cCamera camera)
SDL_SetRenderDrawColor(global.mainRenderer, prevR, prevG, prevB, prevA);
}

/** \brief Automatically updates storedSelection and selection of a textbox based on what you click
*
* \param textBox warperTextBox* - text box you want updated
* \param xClick int - x coordinate of click (based on text box's text rects)
* \param yClick int - y coordinate of click (based on text box's text rects)
*/
void checkWarperTextBoxClick(warperTextBox* textBox, int xClick, int yClick)
{
if (xClick > textBox->rect.x && xClick < textBox->rect.x + textBox->rect.w && yClick > textBox->rect.y && yClick < textBox->rect.y + textBox->rect.h)
{
textBox->storedSelection = textBox->selection;

for(int i = 0; i < textBox->textsSize; i++)
{
if (textBox->isOption[i] && (xClick > textBox->texts[i].rect.x && xClick < textBox->texts[i].rect.x + textBox->texts[i].rect.w &&
yClick > textBox->texts[i].rect.y && yClick < textBox->texts[i].rect.y + textBox->texts[i].rect.h))
{
//we clicked on an element
textBox->selection = i;
}
}
}
}

/** \brief CoSprite helper function; if using, cast textBox to a void*
*
* \param textBoxSubclass void*
Expand Down Expand Up @@ -218,7 +197,7 @@ void drawWarperCircle(void* circle, cCamera camera)
cSprite cheatCircle; //init the circle
initCSprite(&cheatCircle, NULL, "./assets/filledCircleCheat.png", 0,
(cDoubleRect) {wCircle->center.x - wCircle->radius, wCircle->center.y - wCircle->radius, 2 * wCircle->radius, 2 * wCircle->radius},
(cDoubleRect) {0, 0, 507, 507}, NULL, 1.0, SDL_FLIP_NONE, 0, false, NULL, 5);
(cDoubleRect) {0, 0, 507, 507}, NULL, 1.0, SDL_FLIP_NONE, 0, false, false, NULL, 5);

//do some color and alpha modding
SDL_SetTextureColorMod(cheatCircle.texture, wCircle->circleColor.r, wCircle->circleColor.g, wCircle->circleColor.b);
Expand All @@ -230,6 +209,18 @@ void drawWarperCircle(void* circle, cCamera camera)
}
}

void drawCursor(void* cursor, cCamera camera)
{
drawCSprite(cursorSprite, camera, false, false);
if (cursorSprite.id == CURSOR_SELECT)
updateCursorIcon(CURSOR_NORMAL); //update the select sprite back to normal
}

void destroyCursor(void* cursor)
{
//nothing needed so far
}

/** \brief CoSprite helper function; if using, cast circle to a void*
*
* \param circle void*
Expand Down Expand Up @@ -319,6 +310,68 @@ void createMenuTextBox(warperTextBox* textBox, cDoubleRect dimensions, cDoublePt
free(texts);
}

void checkWarperTextBoxHover(warperTextBox* textBox, SDL_MouseMotionEvent motion)
{
if (motion.x >= 0 && motion.y >= 0 && motion.x <= SCREEN_PX_WIDTH && motion.y <= SCREEN_PX_HEIGHT)
{
if (motion.x > textBox->rect.x && motion.x < textBox->rect.x + textBox->rect.w && motion.y > textBox->rect.y && motion.y < textBox->rect.y + textBox->rect.h)
{
for(int i = 0; i < textBox->textsSize; i++)
{
if (textBox->isOption[i] && (motion.x > textBox->texts[i].rect.x && motion.x < textBox->texts[i].rect.x + textBox->texts[i].rect.w &&
motion.y > textBox->texts[i].rect.y && motion.y < textBox->texts[i].rect.y + textBox->texts[i].rect.h))
{
updateCursorIcon(CURSOR_HOVER);
}
}
}
}
}

/** \brief Automatically updates storedSelection and selection of a textbox based on what you click
*
* \param textBox warperTextBox* - text box you want updated
* \param xClick int - x coordinate of click (based on text box's text rects)
* \param yClick int - y coordinate of click (based on text box's text rects)
*/
void checkWarperTextBoxSelection(warperTextBox* textBox, int xClick, int yClick)
{
if (xClick >= textBox->rect.x && xClick <= textBox->rect.x + textBox->rect.w && yClick >= textBox->rect.y && yClick <= textBox->rect.y + textBox->rect.h)
{
textBox->storedSelection = textBox->selection;

for(int i = 0; i < textBox->textsSize; i++)
{
if (textBox->isOption[i] && (xClick > textBox->texts[i].rect.x && xClick < textBox->texts[i].rect.x + textBox->texts[i].rect.w &&
yClick > textBox->texts[i].rect.y && yClick < textBox->texts[i].rect.y + textBox->texts[i].rect.h))
{
//we clicked on an element
textBox->selection = i;
updateCursorIcon(CURSOR_SELECT);
}
}
}
}

void updateCursorPos(SDL_MouseMotionEvent motion, bool debugPrint)
{
if (motion.x > -1 && motion.y > -1 && motion.x <= SCREEN_PX_WIDTH && motion.y <= SCREEN_PX_HEIGHT)
{
if (debugPrint)
printf("%d, %d\n", motion.x, motion.y);
cursorSprite.drawRect.x = motion.x;
cursorSprite.drawRect.y = motion.y;
}
}


void updateCursorIcon(int id)
{
cursorSprite.id = id;
cursorSprite.srcClipRect.x = 16 * (id % 2); //0 if even, 16 if odd
cursorSprite.srcClipRect.y = 64 + 16 * (id / 2); //64 if 0-1, 80 if 2-3, etc.
}

/** \brief
*
* \param tilemap warperTilemap - the tilemap to use
Expand All @@ -340,7 +393,7 @@ void loadGridModel(warperTilemap tilemap, c2DModel* gridModel, Uint8 opacity)
initCSprite(&gridSprites[x * tilemap.height + y], uiTilesetTexture, "assets/uiTilesheet.png", 1,
(cDoubleRect) {tilemap.tileSize * x, tilemap.tileSize * y, tilemap.tileSize, tilemap.tileSize},
(cDoubleRect) {0, 32, 32, 32},
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 5);
NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, NULL, 5);
}
}
initC2DModel(gridModel, gridSprites, tilemap.width * tilemap.height, (cDoublePt) {0, 0}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, NULL, 2);
Expand Down
25 changes: 24 additions & 1 deletion warperInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
#include "warper.h"
#include "battleSystem.h"

#define CURSOR_NORMAL 0
#define CURSOR_ALLY 1
#define CURSOR_NEUTRAL 2
#define CURSOR_ENEMY 3
#define CURSOR_HOVER 4
#define CURSOR_SELECT 5
#define CURSOR_A 6
#define CURSOR_B 7
#define CURSOR_NORMAL_BLOCKED 8
#define CURSOR_ALLY_BLOCKED 9
#define CURSOR_GRAY_BLOCKED 10
#define CURSOR_ENEMY_OK 11

typedef struct _warperTextBox
{
cDoubleRect rect;
Expand Down Expand Up @@ -40,18 +53,28 @@ typedef struct _warperCircle
//CoSprite helper functs:
void initWarperTextBox(warperTextBox* textBox, cDoubleRect rect, SDL_Color outlineColor, SDL_Color bgColor, SDL_Color highlightColor, cText* texts, bool* isOption, int textsSize, bool isMenu);
void drawWarperTextBox(void* textBoxSubclass, cCamera camera);
void checkWarperTextBoxClick(warperTextBox* textBox, int xClick, int yClick);
void destroyWarperTextBox(void* textBoxSubclass);
void drawWarperPath(void* path, cCamera camera);
void destroyWarperPath(void* path);
void drawWarperCircle(void* circle, cCamera camera);
void destroyWarperCircle(void* circle);
void drawCursor(void* cursor, cCamera camera);
void destroyCursor(void* cursor);

//text box helper functs:
void createBattleTextBox(warperTextBox* textBox, cDoubleRect dimensions, cDoublePt margins, double verticalSpacing, bool justify, char** strings, bool* isOptions, int stringsLength, int tileSize);
void createMenuTextBox(warperTextBox* textBox, cDoubleRect dimensions, cDoublePt margins, double verticalSpacing, bool justify, Uint8 bgOpacity, char** strings, bool* isOptions, int stringsLength, cFont* font);
void checkWarperTextBoxHover(warperTextBox* textBox, SDL_MouseMotionEvent motion);
void checkWarperTextBoxSelection(warperTextBox* textBox, int xClick, int yClick);

//cursor
void updateCursorPos(SDL_MouseMotionEvent motion, bool debugPrint);
void updateCursorIcon(int id);

//Other UI/UX
void loadGridModel(warperTilemap tilemap, c2DModel* gridModel, Uint8 opacity);

extern cSprite cursorSprite; //global cursor sprite
extern cResource cursorResource;

#endif // WARPERINTERFACE_H_INCLUDED

0 comments on commit 331b6c1

Please sign in to comment.