Switch to GCC and C++

Also add workspace file
This commit is contained in:
Tobias Berger 2021-11-29 12:08:41 +01:00
parent 4fecba89dd
commit 1a1a73ea9f
8 changed files with 125 additions and 91 deletions

View file

@ -7,10 +7,31 @@
"${workspaceFolder}/raylib/src/extras" "${workspaceFolder}/raylib/src/extras"
], ],
"windowsSdkVersion": "10.0.19041.0", "windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\dev\\tcc\\tcc.exe", "compilerPath": "D:/mingw/mingw32/bin/gcc.exe",
"cStandard": "c17", "cStandard": "c17",
"cppStandard": "c++17", "cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64" "intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
"-Iinclude",
"-Iraylib/src",
"-Iraylib/src/extras",
"-Llib",
"-std=c++2a",
"-Werror",
"-Wall",
"-Wextra",
"-Wno-missing-field-initializers",
"-Wpedantic",
"-pedantic-errors",
"-Wwrite-strings",
"-lraylib",
"-lopengl32",
"-lgdi32",
"-luser32",
"-lshell32",
"-lwinmm",
"-D__DEBUG"
]
} }
], ],
"version": 4 "version": 4

View file

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

88
.vscode/tasks.json vendored
View file

@ -6,28 +6,31 @@
{ {
"label": "Compile (Debug)", "label": "Compile (Debug)",
"type": "shell", "type": "shell",
"command": "tcc", "command": "gcc",
"args": [ "args": [
{ "value": "src/**.c", "quoting": "weak" }, { "value": "src/**.cpp", "quoting": "weak" },
{ "value": "-ooutput.exe", "quoting": "strong" }, { "value": "-ooutput.exe", "quoting": "strong" },
{ "value": "-bench", "quoting": "strong" }, { "value": "-Og", "quoting": "strong" },
{ "value": "-v", "quoting": "strong" }, { "value": "-g", "quoting": "strong" },
{ "value": "-b", "quoting": "strong" }, { "value": "-Iinclude", "quoting": "strong" },
{ "value": "-Iinclude", "quoting": "strong" }, { "value": "-Iraylib/src", "quoting": "strong" },
{ "value": "-Iraylib/src", "quoting": "strong" }, { "value": "-Iraylib/src/extras", "quoting": "strong" },
{ "value": "-Iraylib/src/extras", "quoting": "strong" }, { "value": "-Llib", "quoting": "strong" },
{ "value": "-Llib", "quoting": "strong" }, { "value": "-std=c++2a", "quoting": "strong" },
{ "value": "-stdc99", "quoting": "strong" }, { "value": "-Werror", "quoting": "strong" },
{ "value": "-Wall", "quoting": "strong" }, { "value": "-Wall", "quoting": "strong" },
{ "value": "-Werror", "quoting": "strong" }, { "value": "-Wextra", "quoting": "strong" },
{ "value": "-Wwrite-strings", "quoting": "strong" }, { "value": "-Wno-missing-field-initializers", "quoting": "strong" },
{ "value": "-lraylib", "quoting": "strong" }, { "value": "-Wpedantic", "quoting": "strong" },
{ "value": "-lopengl32", "quoting": "strong" }, { "value": "-pedantic-errors", "quoting": "strong" },
{ "value": "-lgdi32", "quoting": "strong" }, { "value": "-Wwrite-strings", "quoting": "strong" },
{ "value": "-luser32", "quoting": "strong" }, { "value": "-lraylib", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" }, { "value": "-lopengl32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" }, { "value": "-lgdi32", "quoting": "strong" },
{ "value": "-D__DEBUG", "quoting": "strong" }, { "value": "-luser32", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" },
{ "value": "-D__DEBUG", "quoting": "strong" },
], ],
"group": { "group": {
"kind": "build", "kind": "build",
@ -37,27 +40,30 @@
{ {
"label": "Compile (Release)", "label": "Compile (Release)",
"type": "shell", "type": "shell",
"command": "tcc", "command": "gcc",
"args": [ "args": [
{ "value": "src/**.c", "quoting": "weak" }, { "value": "src/**.cpp", "quoting": "weak" },
{ "value": "-ooutput.exe", "quoting": "strong" }, { "value": "-ooutput.exe", "quoting": "strong" },
{ "value": "-bench", "quoting": "strong" }, { "value": "-Ofast", "quoting": "strong" },
{ "value": "-v", "quoting": "strong" }, { "value": "-Iinclude", "quoting": "strong" },
{ "value": "-Iinclude", "quoting": "strong" }, { "value": "-Iraylib/src", "quoting": "strong" },
{ "value": "-Iraylib/src", "quoting": "strong" }, { "value": "-Iraylib/src/extras", "quoting": "strong" },
{ "value": "-Iraylib/src/extras", "quoting": "strong" }, { "value": "-Llib", "quoting": "strong" },
{ "value": "-Llib", "quoting": "strong" }, { "value": "-std=c++2a", "quoting": "strong" },
{ "value": "-stdc99", "quoting": "strong" }, { "value": "-Werror", "quoting": "strong" },
{ "value": "-Wall", "quoting": "strong" }, { "value": "-Wall", "quoting": "strong" },
{ "value": "-Werror", "quoting": "strong" }, { "value": "-Wextra", "quoting": "strong" },
{ "value": "-Wwrite-strings", "quoting": "strong" }, { "value": "-Wno-missing-field-initializers", "quoting": "strong" },
{ "value": "-lraylib", "quoting": "strong" }, { "value": "-Wpedantic", "quoting": "strong" },
{ "value": "-lopengl32", "quoting": "strong" }, { "value": "-pedantic-errors", "quoting": "strong" },
{ "value": "-lgdi32", "quoting": "strong" }, { "value": "-Wwrite-strings", "quoting": "strong" },
{ "value": "-luser32", "quoting": "strong" }, { "value": "-lraylib", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" }, { "value": "-lopengl32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" }, { "value": "-lgdi32", "quoting": "strong" },
{ "value": "-Wl,--subsystem,windows", "quoting": "strong" }, { "value": "-luser32", "quoting": "strong" },
{ "value": "-lshell32", "quoting": "strong" },
{ "value": "-lwinmm", "quoting": "strong" },
{ "value": "-Wl,--subsystem,windows", "quoting": "strong" },
], ],
"group": "build", "group": "build",
} }

12
.vscode/workspace.code-workspace vendored Normal file
View file

@ -0,0 +1,12 @@
{
"folders": [
{
"path": ".."
}
],
"extensions": {
"recommendations": [
"ms-vscode.cpptools"
]
}
}

View file

@ -6,9 +6,9 @@ void fileToBytes(const char *inputFileName, const char *outputFileName, const ch
{ {
unsigned int fileSize = 0; unsigned int fileSize = 0;
unsigned char *fileData = LoadFileData(inputFileName, &fileSize); unsigned char *const fileData = LoadFileData(inputFileName, &fileSize);
char *content = (char *)MemAlloc(sizeof(char) * fileSize * 10); char *const content = (char *const)MemAlloc(sizeof(char) * fileSize * 10);
int cursor = 0; int cursor = 0;
@ -18,9 +18,9 @@ void fileToBytes(const char *inputFileName, const char *outputFileName, const ch
} }
cursor = 0; cursor = 0;
char *final = (char *)MemAlloc((sizeof("const unsigned char ") + sizeof("[] = {\n") + (fileSize * sizeof(" 0x00,\n")) + sizeof("};") + strlen(arrayName)) * sizeof(char)); char *const final = (char *const)MemAlloc((sizeof("static const unsigned char ") + strlen(arrayName) + sizeof("[] = {\n") + (fileSize * sizeof(" 0x00,\n")) + sizeof("};")) * sizeof(char));
TextAppend(final, "const unsigned char ", &cursor); TextAppend(final, "static const unsigned char ", &cursor);
TextAppend(final, arrayName, &cursor); TextAppend(final, arrayName, &cursor);
TextAppend(final, TextFormat("[%d] = {\n", fileSize), &cursor); TextAppend(final, TextFormat("[%d] = {\n", fileSize), &cursor);
TextAppend(final, content, &cursor); TextAppend(final, content, &cursor);

View file

@ -1,4 +1,4 @@
const unsigned char monogramFontBytes[] = { static const unsigned char monogramFontBytes[] = {
0x00, 0x00,
0x01, 0x01,
0x00, 0x00,

2
raylib

@ -1 +1 @@
Subproject commit ff2627dd10f1a6d013001611ae7947bee96aefeb Subproject commit 0851960397f02a477d80eda2239f90fae14dec64

View file

@ -2,59 +2,56 @@
#include "raymath.h" #include "raymath.h"
#include "monogram.ttf.h" #include "monogram.ttf.h"
// Size of a gameboard pixel enum class Player
#define SCREEN_SCALE 256
#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, NONE,
RED_PLAYER, RED_PLAYER,
BLUE_PLAYER, BLUE_PLAYER,
} Player; };
Player checkBoard(Player board[9]) Player checkBoard(Player board[9])
{ {
// horizontal, vertical, and diagonal checks from first cell // horizontal, vertical, and diagonal checks from first cell
if (board[0] != NONE && ((board[0] == board[1] && board[0] == board[2]) || (board[0] == board[3] && board[0] == board[6]) || (board[0] == board[4] && board[0] == board[8]))) if (board[0] != Player::NONE && ((board[0] == board[1] && board[0] == board[2]) || (board[0] == board[3] && board[0] == board[6]) || (board[0] == board[4] && board[0] == board[8])))
return board[0]; return board[0];
// vertical check from second cell // vertical check from second cell
if (board[1] != NONE && (board[1] == board[4] && board[1] == board[7])) if (board[1] != Player::NONE && (board[1] == board[4] && board[1] == board[7]))
return board[1]; return board[1];
// vertical and diagonal checks from third cell // vertical and diagonal checks from third cell
if (board[2] != NONE && ((board[2] == board[5] && board[2] == board[8]) || (board[2] == board[4] && board[2] == board[6]))) if (board[2] != Player::NONE && ((board[2] == board[5] && board[2] == board[8]) || (board[2] == board[4] && board[2] == board[6])))
return board[2]; return board[2];
// horizontal check from fourth cell // horizontal check from fourth cell
if (board[3] != NONE && (board[3] == board[4] && board[3] == board[5])) if (board[3] != Player::NONE && (board[3] == board[4] && board[3] == board[5]))
return board[3]; return board[3];
// horizontal check from seventh cell // horizontal check from seventh cell
if (board[6] != NONE && (board[6] == board[7] && board[6] == board[8])) if (board[6] != Player::NONE && (board[6] == board[7] && board[6] == board[8]))
return board[6]; return board[6];
return NONE; return Player::NONE;
} }
void resetBoard(Player board[9]) void resetBoard(Player board[9])
{ {
for (unsigned char i = 0; i < 9; i++) for (unsigned char i = 0; i < 9; i++)
{ {
board[i] = NONE; board[i] = Player::NONE;
} }
} }
int main(void) int main(void)
{ {
// Size of a gameboard pixel
static const int SCREEN_SCALE = 256;
static const int TOPBAR_HEIGHT = 40;
static const int VIRTUAL_SCREEN_SIZE = 3;
static const int SCREEN_SIZE = VIRTUAL_SCREEN_SIZE * SCREEN_SCALE;
static const float TEXT_SPACING = 2.0f;
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
#if defined(__DEBUG) #if defined(__DEBUG)
@ -66,8 +63,8 @@ int main(void)
SetConfigFlags(0); SetConfigFlags(0);
InitWindow(SCREEN_SIZE, SCREEN_SIZE + TOPBAR_HEIGHT, "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', ' '}; static const int fontChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
Font monogramFont = LoadFontFromMemory(".ttf", monogramFontBytes, sizeof(monogramFontBytes), TOPBAR_HEIGHT, fontChars, sizeof(fontChars)); Font monogramFont = LoadFontFromMemory(".ttf", monogramFontBytes, sizeof(monogramFontBytes), TOPBAR_HEIGHT, (int *)fontChars, sizeof(fontChars));
/* For drawing */ /* For drawing */
RenderTexture2D target = LoadRenderTexture(VIRTUAL_SCREEN_SIZE, VIRTUAL_SCREEN_SIZE); RenderTexture2D target = LoadRenderTexture(VIRTUAL_SCREEN_SIZE, VIRTUAL_SCREEN_SIZE);
@ -82,9 +79,9 @@ int main(void)
SCREEN_SIZE}; SCREEN_SIZE};
/* For game logic */ /* For game logic */
Player board[9] = {0}; Player board[9];
resetBoard(board); resetBoard(board);
Player currentPlayer = RED_PLAYER; Player currentPlayer = Player::RED_PLAYER;
unsigned char points[3] = {0, 0, 0}; unsigned char points[3] = {0, 0, 0};
unsigned char turn = 0; unsigned char turn = 0;
@ -113,22 +110,22 @@ int main(void)
// Floor instead of trunc to check negative values for out-of-bounds check for cursor // 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)TOPBAR_HEIGHT)) / (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) if (hovered < 0 || hovered >= 9 || board[(unsigned char)hovered] != Player::NONE)
{ {
SetMouseCursor(MOUSE_CURSOR_DEFAULT); SetMouseCursor(MOUSE_CURSOR_DEFAULT);
} }
else if (board[hovered] == NONE) else if (board[(unsigned char)hovered] == Player::NONE)
{ {
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{ {
board[hovered] = currentPlayer; board[(unsigned char)hovered] = currentPlayer;
currentPlayer = (currentPlayer == RED_PLAYER) ? BLUE_PLAYER : RED_PLAYER; currentPlayer = (currentPlayer == Player::RED_PLAYER) ? Player::BLUE_PLAYER : Player::RED_PLAYER;
// Cursor doesn't really matter in this frame // Cursor doesn't really matter in this frame
Player winner = checkBoard(board); Player winner = checkBoard(board);
// Game is over // Game is over
if (winner != NONE || (++turn) >= 9) if (winner != Player::NONE || (++turn) >= 9)
{ {
TraceLog(LOG_INFO, TextFormat("Winner: %d", winner)); TraceLog(LOG_INFO, TextFormat("Winner: %d", winner));
points[(int)winner]++; points[(int)winner]++;
@ -152,16 +149,17 @@ int main(void)
for (unsigned char i = 0; i < 9; i++) for (unsigned char i = 0; i < 9; i++)
{ {
Color color = {0}; Color color;
switch (board[i]) switch (board[i])
{ {
case RED_PLAYER: case Player::RED_PLAYER:
color = RED; color = RED;
break; break;
case BLUE_PLAYER: case Player::BLUE_PLAYER:
color = BLUE; color = BLUE;
break; break;
case Player::NONE:
default: default:
color = WHITE; color = WHITE;
break; break;
@ -173,7 +171,7 @@ int main(void)
if (i == hovered) if (i == hovered)
{ {
DrawPixel(x, y, (Color){255, 255, 255, 128}); DrawPixel(x, y, Color{255, 255, 255, 128});
} }
} }
} }
@ -184,7 +182,7 @@ int main(void)
ClearBackground(BLACK); ClearBackground(BLACK);
// Draw rendered game texture // Draw rendered game texture
DrawTexturePro(target.texture, sourceRec, destRec, (Vector2){0.0f, 0.0f}, 0.0f, WHITE); DrawTexturePro(target.texture, sourceRec, destRec, Vector2{0.0f, 0.0f}, 0.0f, WHITE);
#if __DEBUG #if __DEBUG
DrawFPS(0, TOPBAR_HEIGHT); DrawFPS(0, TOPBAR_HEIGHT);
@ -195,12 +193,14 @@ int main(void)
// Current player in top-left corner // Current player in top-left corner
switch (currentPlayer) switch (currentPlayer)
{ {
case RED_PLAYER: case Player::RED_PLAYER:
DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, RED); DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, RED);
break; break;
case BLUE_PLAYER: case Player::BLUE_PLAYER:
DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, BLUE); DrawRectangle(0, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, BLUE);
break; break;
default:
break;
} }
// Scores in top-left, labelled with colored squares // Scores in top-left, labelled with colored squares
@ -213,7 +213,7 @@ int main(void)
const char *scoreboard = TextFormat("%03u %s", points[0], redScore); const char *scoreboard = TextFormat("%03u %s", points[0], redScore);
const int scoreboardWidth = (int)MeasureTextEx(monogramFont, scoreboard, TOPBAR_HEIGHT, TEXT_SPACING).x; const int scoreboardWidth = (int)MeasureTextEx(monogramFont, scoreboard, TOPBAR_HEIGHT, TEXT_SPACING).x;
DrawTextEx(monogramFont, scoreboard, (Vector2){SCREEN_SIZE - scoreboardWidth, 0}, TOPBAR_HEIGHT, TEXT_SPACING, WHITE); DrawTextEx(monogramFont, scoreboard, Vector2{(float)SCREEN_SIZE - scoreboardWidth, 0}, TOPBAR_HEIGHT, TEXT_SPACING, WHITE);
DrawRectangle(SCREEN_SIZE - TOPBAR_HEIGHT - blueScoreWidth - 2, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, BLUE); 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 - redScoreWidth - 2, 0, TOPBAR_HEIGHT, TOPBAR_HEIGHT, RED);