Fix launch, file to unsigned char[]

This commit is contained in:
Tobias Berger 2021-11-24 14:36:30 +01:00
parent 647dc39920
commit 4fecba89dd
6 changed files with 41572 additions and 17 deletions

2
.vscode/launch.json vendored
View file

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/output.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",

View file

@ -1,5 +1,10 @@
{
"C_Cpp.default.defines": [
"__DEBUG"
]
],
"files.associations": {
"*.lock": "yarnlock",
"monogram.h": "c",
"*.m": "cpp"
}
}

34
include/fileToCharArray.h Normal file
View file

@ -0,0 +1,34 @@
#include "raylib.h"
#include <string.h>
void fileToBytes(const char *inputFileName, const char *outputFileName, const char *arrayName)
{
unsigned int fileSize = 0;
unsigned char *fileData = LoadFileData(inputFileName, &fileSize);
char *content = (char *)MemAlloc(sizeof(char) * fileSize * 10);
int cursor = 0;
for (unsigned int i = 0; i < fileSize; i++)
{
TextAppend(content, TextFormat(" 0x%02x,\n", fileData[i]), &cursor);
}
cursor = 0;
char *final = (char *)MemAlloc((sizeof("const unsigned char ") + sizeof("[] = {\n") + (fileSize * sizeof(" 0x00,\n")) + sizeof("};") + strlen(arrayName)) * sizeof(char));
TextAppend(final, "const unsigned char ", &cursor);
TextAppend(final, arrayName, &cursor);
TextAppend(final, TextFormat("[%d] = {\n", fileSize), &cursor);
TextAppend(final, content, &cursor);
TextAppend(final, " };", &cursor);
MemFree(content);
SaveFileText(outputFileName, final);
MemFree(final);
}

41510
include/monogram.ttf.h Normal file

File diff suppressed because it is too large Load diff

BIN
resources/arial10x10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View file

@ -1,15 +1,18 @@
#include "raylib.h"
#include "raymath.h"
#include "monogram.ttf.h"
// Size of a gameboard pixel
#define SCREEN_SCALE 256
#define TOP_OFFSET 40
#define TOPBAR_HEIGHT 40
#define SCREEN_SIZE (SCREEN_SCALE * 3)
#define VIRTUAL_SCREEN_SIZE SCREEN_SIZE / SCREEN_SCALE
#define TEXT_SPACING 2.0f
typedef enum Player
{
NONE,
@ -61,7 +64,10 @@ int main(void)
#endif
SetConfigFlags(0);
InitWindow(SCREEN_SIZE, SCREEN_SIZE + TOP_OFFSET, "Tic Tac Toe");
InitWindow(SCREEN_SIZE, SCREEN_SIZE + TOPBAR_HEIGHT, "Tic Tac Toe");
int fontChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
Font monogramFont = LoadFontFromMemory(".ttf", monogramFontBytes, sizeof(monogramFontBytes), TOPBAR_HEIGHT, fontChars, sizeof(fontChars));
/* For drawing */
RenderTexture2D target = LoadRenderTexture(VIRTUAL_SCREEN_SIZE, VIRTUAL_SCREEN_SIZE);
@ -71,7 +77,7 @@ int main(void)
(float)VIRTUAL_SCREEN_SIZE,
-(float)VIRTUAL_SCREEN_SIZE};
Rectangle destRec = {0.0f,
(float)(TOP_OFFSET),
(float)(TOPBAR_HEIGHT),
SCREEN_SIZE,
SCREEN_SIZE};
@ -105,7 +111,7 @@ int main(void)
Vector2 mousePos = GetMousePosition();
// Floor instead of trunc to check negative values for out-of-bounds check for cursor
char hovered = (char)floorf(mousePos.x / (float)SCREEN_SCALE) + 3 * (char)floorf((mousePos.y - ((float)TOP_OFFSET)) / (float)SCREEN_SCALE);
char hovered = (char)floorf(mousePos.x / (float)SCREEN_SCALE) + 3 * (char)floorf((mousePos.y - ((float)TOPBAR_HEIGHT)) / (float)SCREEN_SCALE);
if (hovered < 0 || hovered >= 9 || board[hovered] != NONE)
{
@ -181,7 +187,7 @@ int main(void)
DrawTexturePro(target.texture, sourceRec, destRec, (Vector2){0.0f, 0.0f}, 0.0f, WHITE);
#if __DEBUG
DrawFPS(0, TOP_OFFSET);
DrawFPS(0, TOPBAR_HEIGHT);
#endif
// Draw Top Bar
@ -190,28 +196,28 @@ int main(void)
switch (currentPlayer)
{
case RED_PLAYER:
DrawRectangle(0, 0, TOP_OFFSET, TOP_OFFSET, RED);
DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, RED);
break;
case BLUE_PLAYER:
DrawRectangle(0, 0, TOP_OFFSET, TOP_OFFSET, BLUE);
DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, BLUE);
break;
}
// Scores in top-left, labelled with colored squares
const char *blueScore = TextFormat("%03u", points[2]);
const int blueScoreWidth = MeasureText(blueScore, TOP_OFFSET);
const char *blueScore = TextFormat("%03u ", points[2]);
const int blueScoreWidth = (int)MeasureTextEx(monogramFont, blueScore, TOPBAR_HEIGHT, TEXT_SPACING).x;
const char *redScore = TextFormat("%03u %s", points[1], blueScore);
const int redScoreWidth = MeasureText(redScore, TOP_OFFSET);
const int redScoreWidth = (int)MeasureTextEx(monogramFont, redScore, TOPBAR_HEIGHT, TEXT_SPACING).x;
const char *scoreboard = TextFormat("%03u %s", points[0], redScore);
const int scoreboardWidth = MeasureText(scoreboard, TOP_OFFSET);
const int scoreboardWidth = (int)MeasureTextEx(monogramFont, scoreboard, TOPBAR_HEIGHT, TEXT_SPACING).x;
DrawText(scoreboard, SCREEN_SIZE - scoreboardWidth, 0, TOP_OFFSET, WHITE);
DrawTextEx(monogramFont, scoreboard, (Vector2){SCREEN_SIZE - scoreboardWidth, 0}, TOPBAR_HEIGHT, TEXT_SPACING, WHITE);
DrawRectangle(SCREEN_SIZE - TOP_OFFSET - blueScoreWidth - 2, 0, TOP_OFFSET, TOP_OFFSET, BLUE);
DrawRectangle(SCREEN_SIZE - TOP_OFFSET - redScoreWidth - 2, 0, TOP_OFFSET, TOP_OFFSET, RED);
DrawRectangle(SCREEN_SIZE - TOP_OFFSET - scoreboardWidth - 2, 0, TOP_OFFSET, TOP_OFFSET, WHITE);
DrawRectangle(SCREEN_SIZE - TOPBAR_HEIGHT - blueScoreWidth - 2, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, BLUE);
DrawRectangle(SCREEN_SIZE - TOPBAR_HEIGHT - redScoreWidth - 2, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, RED);
DrawRectangle(SCREEN_SIZE - TOPBAR_HEIGHT - scoreboardWidth - 2, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, WHITE);
}
EndDrawing();
//----------------------------------------------------------------------------------