aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <luca@spacehb.net>2025-07-10 15:32:21 +0200
committerRaymaekers Luca <luca@spacehb.net>2025-07-10 15:32:21 +0200
commit4ab197add10847d4ba3036ed525f96db00a9849f (patch)
treefd057771c14cb5d611d7d9bafed7fdec6d5f253a
parente239c0a12909e434efc1854d8992df145c567e4a (diff)
checkpoint
-rw-r--r--.gitignore1
-rwxr-xr-xbuild/handmade.sobin216280 -> 222136 bytes
-rwxr-xr-xbuild/linux_handmadebin90472 -> 91512 bytes
-rwxr-xr-xcode/build.sh2
-rw-r--r--code/handmade.cpp69
-rw-r--r--code/handmade.h1
-rw-r--r--code/handmade_platform.h10
-rw-r--r--code/linux_handmade.cpp69
-rwxr-xr-xmisc/package_linux.sh11
-rw-r--r--todo.txt9
10 files changed, 150 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
index f8b680a..ca10216 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
build/*.hmi
+game.zip
diff --git a/build/handmade.so b/build/handmade.so
index 83c26e3..3cb284d 100755
--- a/build/handmade.so
+++ b/build/handmade.so
Binary files differ
diff --git a/build/linux_handmade b/build/linux_handmade
index 9f53a09..a304d6e 100755
--- a/build/linux_handmade
+++ b/build/linux_handmade
Binary files differ
diff --git a/code/build.sh b/code/build.sh
index 9be3b65..3380b73 100755
--- a/code/build.sh
+++ b/code/build.sh
@@ -5,7 +5,7 @@ cd "$ThisDir"
mkdir ../build > /dev/null 2>&1
-CompilerFlags="-ggdb -DHANDMADE_INTERNAL -DHANDMADE_SLOW"
+CompilerFlags="-ggdb -DHANDMADE_SLOW"
WarningFlags="-Wall -Wextra -Wno-unused-but-set-variable -Wno-unused-variable -Wno-write-strings -Wno-unused-parameter -Wno-unused-function"
printf 'handmade.cpp\n'
diff --git a/code/handmade.cpp b/code/handmade.cpp
index 15abc6e..b5fa5c5 100644
--- a/code/handmade.cpp
+++ b/code/handmade.cpp
@@ -7,9 +7,9 @@
#include "libs/stb_truetype.h"
#endif
-#if HANDMADE_INTERNAL
+// TODO(luca): Get rid of these.
+#include <time.h>
#include <stdio.h>
-#endif
internal s16
GetSineSound(u32 SampleRate)
@@ -427,6 +427,63 @@ ValidLetterCountInGuess(char *Word, char *Guess, char Letter)
return Valid;
}
+internal void
+GetTodaysWordle(thread_context *Thread, game_memory *Memory, char *Word)
+{
+ struct tm *LocalTimeNow = 0;
+ time_t Now = 0;
+ time(&Now);
+ LocalTimeNow = localtime(&Now);
+
+ char URL[] = "https://www.nytimes.com/svc/wordle/v2";
+ char URLBuffer[256] = {0};
+ sprintf(URLBuffer, "%s/%d-%02d-%d.json", URL,
+ LocalTimeNow->tm_year + 1900, LocalTimeNow->tm_mon + 1, LocalTimeNow->tm_mday);
+ char OutputBuffer[4096] = {0};
+ char *Command[] = {"/usr/bin/curl", "-sL", URLBuffer, 0};
+
+ int BytesOutputted = Memory->PlatformRunCommandAndGetOutput(Thread, OutputBuffer, Command);
+ int Matches;
+ int MatchedAt = 0;
+
+ for(int At = 0;
+ At < BytesOutputted;
+ At++)
+ {
+ Matches = true;
+ char ScanMatch[] = "solution";
+ int ScanMatchSize = sizeof(ScanMatch) - 1;
+ for(int ScanAt = 0;
+ ScanAt < ScanMatchSize;
+ ScanAt++)
+ {
+ if((OutputBuffer + At)[ScanAt] != ScanMatch[ScanAt])
+ {
+ Matches = false;
+ break;
+ }
+ }
+ if(Matches)
+ {
+ MatchedAt = At;
+ break;
+ }
+ }
+ if(Matches)
+ {
+ int Start = 0;
+ int End = 0;
+ int Scan = MatchedAt;
+ while(OutputBuffer[Scan++] != '"' && Scan < BytesOutputted);
+ while(OutputBuffer[Scan++] != '"' && Scan < BytesOutputted);
+ Start = Scan;
+ while(OutputBuffer[Scan] != '"' && Scan < BytesOutputted) Scan++;
+ End = Scan;
+
+ memcpy(Word, OutputBuffer+Start, End-Start);
+ }
+}
+
extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
{
Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) ==
@@ -438,6 +495,7 @@ extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
if(!Memory->IsInitialized)
{
debug_read_file_result File = Memory->DEBUGPlatformReadEntireFile(Thread, "../data/font.ttf");
+
if(stbtt_InitFont(&GameState->FontInfo, (u8 *)File.Contents, stbtt_GetFontOffsetForIndex((u8 *)File.Contents, 0)))
{
GameState->FontInfo.data = (u8 *)File.Contents;
@@ -456,6 +514,7 @@ extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
GameState->SelectedColor = SquareColor_Yellow;
GameState->ExportedPatternIndex = 0;
+ GetTodaysWordle(Thread, Memory, GameState->WordleWord);
Memory->IsInitialized = true;
}
@@ -585,15 +644,15 @@ extern "C" GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
TextOffset = v2{16.0f, 16.0f + Baseline};
{
- char Text[] = "\"jumpy\"";
- int TextLen = sizeof(Text) - 1;
+ char Text[WORDLE_LENGTH + 2 + 1] = {};
+ int TextLen = sprintf(Text, "\"%s\"", GameState->WordleWord);
DrawText(GameState, Buffer, FontScale, Text, TextLen, TextOffset + -v2{8.0f, 0.0f}, ColorYellow);
}
TextOffset.Y += YAdvance*2.0f;
//-Matche the pattern
- char *Word = "jumpy";
+ char *Word = GameState->WordleWord;
debug_read_file_result File = Memory->DEBUGPlatformReadEntireFile(Thread, "../data/words.txt");
int WordsCount = File.ContentsSize / WORDLE_LENGTH;
diff --git a/code/handmade.h b/code/handmade.h
index 59d3330..42910fc 100644
--- a/code/handmade.h
+++ b/code/handmade.h
@@ -78,6 +78,7 @@ struct game_state
u32 PatternGrid[6][5];
u32 SelectedColor;
u32 ExportedPatternIndex;
+ char WordleWord[WORDLE_LENGTH];
stbtt_fontinfo FontInfo;
s32 FontAscent;
diff --git a/code/handmade_platform.h b/code/handmade_platform.h
index 4cdf388..2eef88b 100644
--- a/code/handmade_platform.h
+++ b/code/handmade_platform.h
@@ -61,7 +61,6 @@ extern "C" {
#define NullExpression { int X = 4; }
-
#define Kilobytes(Value) ((Value)*1024LL)
#define Megabytes(Value) (Kilobytes(Value)*1024LL)
#define Gigabytes(Value) (Megabytes(Value)*1024LL)
@@ -70,7 +69,6 @@ extern "C" {
#define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
// TODO(casey): swap, min, max ... macros???
-
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
@@ -87,7 +85,6 @@ extern "C" {
typedef float r32;
typedef double r64;
-
typedef struct thread_context
{
int Placeholder;
@@ -96,7 +93,6 @@ extern "C" {
/*
NOTE(casey): Services that the platform layer provides to the game
*/
-#if HANDMADE_INTERNAL
/* IMPORTANT(casey):
These are NOT for doing anything in the shipping game - they are
@@ -117,7 +113,8 @@ extern "C" {
#define DEBUG_PLATFORM_WRITE_ENTIRE_FILE(name) b32 name(thread_context *Thread, char *FileName, u32 MemorySize, void *Memory)
typedef DEBUG_PLATFORM_WRITE_ENTIRE_FILE(debug_platform_write_entire_file);
-#endif
+#define PLATFORM_RUN_COMMAND_AND_GET_OUTPUT(name) b32 name(thread_context *Thread, char *OutputBuffer, char *Command[])
+ typedef PLATFORM_RUN_COMMAND_AND_GET_OUTPUT(platform_run_command_and_get_output);
/*
NOTE(casey): Services that the game provides to the platform layer.
@@ -220,11 +217,10 @@ extern "C" {
memory_index TransientStorageSize;
void *TransientStorage; // NOTE(casey): REQUIRED to be cleared to zero at startup
-#if HANDMADE_INTERNAL
debug_platform_free_file_memory *DEBUGPlatformFreeFileMemory;
debug_platform_read_entire_file *DEBUGPlatformReadEntireFile;
debug_platform_write_entire_file *DEBUGPlatformWriteEntireFile;
-#endif
+ platform_run_command_and_get_output *PlatformRunCommandAndGetOutput;
} game_memory;
#define GAME_UPDATE_AND_RENDER(name) void name(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)
diff --git a/code/linux_handmade.cpp b/code/linux_handmade.cpp
index bf33f72..ba75aed 100644
--- a/code/linux_handmade.cpp
+++ b/code/linux_handmade.cpp
@@ -1,14 +1,14 @@
#include <stdio.h>
-#include <time.h>
-#include <x86intrin.h>
#include <string.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
+#include <x86intrin.h>
#include <dlfcn.h>
#include <fcntl.h>
+
#include <unistd.h>
#include <dirent.h>
-
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
#include <signal.h>
#include <linux/limits.h>
#include <linux/input.h>
@@ -629,6 +629,62 @@ DEBUG_PLATFORM_READ_ENTIRE_FILE(DEBUGPlatformReadEntireFile)
return Result;
}
+PLATFORM_RUN_COMMAND_AND_GET_OUTPUT(PlatformRunCommandAndGetOutput)
+{
+ int Result = 0;
+
+ int HandlesLink[2] = {0};
+ int WaitStatus = 0;
+ pid_t ChildPID = 0;
+ int Ret = 0;
+
+ char *FilePath = Command[0];
+ int AccessMode = F_OK | X_OK;
+ Ret = access(FilePath, AccessMode);
+
+ if(Ret == 0)
+ {
+ Ret = pipe(HandlesLink);
+ if(Ret != -1)
+ {
+ ChildPID = fork();
+ if(ChildPID != -1)
+ {
+ if(ChildPID == 0)
+ {
+ dup2(HandlesLink[1], STDOUT_FILENO);
+ execve(Command[0], Command, 0);
+ }
+ else
+ {
+ wait(&WaitStatus);
+
+ Result = read(HandlesLink[0], OutputBuffer, 4096);
+ if(Result == -1)
+ {
+ Result = 0;
+ }
+ }
+
+ }
+ else
+ {
+ // TODO: Logging
+ }
+ }
+ else
+ {
+ // TODO: Logging
+ }
+ }
+ else
+ {
+ // TODO: Logging
+ }
+
+ return Result;
+}
+
DEBUG_PLATFORM_FREE_FILE_MEMORY(DEBUGPlatformFreeFileMemory)
{
munmap(Memory, MemorySize);
@@ -754,7 +810,9 @@ int main(int ArgC, char *Args[])
{
XSetWindowAttributes WindowAttributes = {};
WindowAttributes.bit_gravity = StaticGravity;
+#if HANDMADE_INTERNAL
WindowAttributes.background_pixel = 0xFF00FF;
+#endif
WindowAttributes.colormap = XCreateColormap(DisplayHandle, RootWindow, WindowVisualInfo.visual, AllocNone);
WindowAttributes.event_mask = (StructureNotifyMask |
KeyPressMask | KeyReleaseMask |
@@ -805,6 +863,7 @@ int main(int ArgC, char *Args[])
GameMemory.DEBUGPlatformReadEntireFile = DEBUGPlatformReadEntireFile;
GameMemory.DEBUGPlatformFreeFileMemory = DEBUGPlatformFreeFileMemory;
GameMemory.DEBUGPlatformWriteEntireFile = DEBUGPlatformWriteEntireFile;
+ GameMemory.PlatformRunCommandAndGetOutput = PlatformRunCommandAndGetOutput;
#if HANDMADE_INTERNAL
void *BaseAddress = (void *)Terabytes(2);
diff --git a/misc/package_linux.sh b/misc/package_linux.sh
new file mode 100755
index 0000000..1acd0c1
--- /dev/null
+++ b/misc/package_linux.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+ThisDir="$(dirname "$(readlink -f "$0")")"
+cd "$ThisDir"
+
+Zip="game.zip"
+
+cd ..
+rm -f "$Zip"
+zip "$Zip" build/linux_handmade build/handmade.so
+zip "$Zip" data/font.ttf data/words.txt
diff --git a/todo.txt b/todo.txt
index 11ad226..cacee93 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,5 +1,6 @@
-- Find a better name for 'view'
-
-- Bug: When using 'jolly', the pattern does not work
+- Get word asynchronously.
+ - Cache it with timestamp.
- Export: pattern to file
-- Testing \ No newline at end of file
+- Testing
+ - Bug: When using 'jolly', the pattern does not work
+x Get word of the day with API. \ No newline at end of file