add SDL3 repository to game engine
Some checks failed
Build (All) / Create test plan (push) Has been cancelled
Build (All) / level1 (push) Has been cancelled
Build (All) / level2 (push) Has been cancelled

This commit is contained in:
Colin Sames
2026-04-03 16:14:58 +02:00
parent f3a3b4b95a
commit f4255e15bb
2038 changed files with 949261 additions and 949254 deletions

View File

@@ -1,4 +1,4 @@
This example code reports power status (plugged in, battery level, etc).
Note that only Chrome-based browsers support this API currently. Firefox and
Safari will report this as unknown, but this may change later!
This example code reports power status (plugged in, battery level, etc).
Note that only Chrome-based browsers support this API currently. Firefox and
Safari will report this as unknown, but this may change later!

View File

@@ -1,155 +1,155 @@
/*
* This example code reports power status (plugged in, battery level, etc).
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */
/* Query for battery info */
int seconds = 0, percent = 0;
const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
/* We set up different drawing details for each power state, then
run it all through the same drawing code. */
int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */
int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */
int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */
int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */
const char *msg = NULL;
const char *msg2 = NULL;
switch (state) {
case SDL_POWERSTATE_ERROR:
msg2 = "ERROR GETTING POWER STATE";
msg = SDL_GetError();
clearr = 255; /* red background */
break;
default: /* in case this does something unexpected later, treat it as unknown. */
case SDL_POWERSTATE_UNKNOWN:
msg = "Power state is unknown.";
clearr = clearb = clearg = 50; /* grey background */
break;
case SDL_POWERSTATE_ON_BATTERY:
msg = "Running on battery.";
barr = 255; /* draw in red */
break;
case SDL_POWERSTATE_NO_BATTERY:
msg = "Plugged in, no battery available.";
clearg = 50; /* green background */
break;
case SDL_POWERSTATE_CHARGING:
msg = "Charging.";
barb = barg = 255; /* draw in cyan */
break;
case SDL_POWERSTATE_CHARGED:
msg = "Charged.";
barg = 255; /* draw in green */
break;
}
SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255);
SDL_RenderClear(renderer);
if (percent >= 0) {
float x, y;
SDL_FRect pctrect;
char remainstr[64];
char msgbuf[128];
SDL_copyp(&pctrect, &frame);
pctrect.w *= percent / 100.0f;
if (seconds < 0) {
SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr));
} else {
int hours, minutes;
hours = seconds / (60 * 60);
seconds -= hours * (60 * 60);
minutes = seconds / 60;
seconds -= minutes * 60;
SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds);
}
SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr);
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f);
y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */
SDL_RenderFillRect(renderer, &pctrect);
SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */
SDL_RenderRect(renderer, &frame);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */
}
if (msg) {
const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msg);
}
if (msg2) {
const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f);
const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msg2);
}
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
/* SDL will clean up the window/renderer for us. */
}
/*
* This example code reports power status (plugged in, battery level, etc).
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */
/* Query for battery info */
int seconds = 0, percent = 0;
const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
/* We set up different drawing details for each power state, then
run it all through the same drawing code. */
int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */
int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */
int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */
int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */
const char *msg = NULL;
const char *msg2 = NULL;
switch (state) {
case SDL_POWERSTATE_ERROR:
msg2 = "ERROR GETTING POWER STATE";
msg = SDL_GetError();
clearr = 255; /* red background */
break;
default: /* in case this does something unexpected later, treat it as unknown. */
case SDL_POWERSTATE_UNKNOWN:
msg = "Power state is unknown.";
clearr = clearb = clearg = 50; /* grey background */
break;
case SDL_POWERSTATE_ON_BATTERY:
msg = "Running on battery.";
barr = 255; /* draw in red */
break;
case SDL_POWERSTATE_NO_BATTERY:
msg = "Plugged in, no battery available.";
clearg = 50; /* green background */
break;
case SDL_POWERSTATE_CHARGING:
msg = "Charging.";
barb = barg = 255; /* draw in cyan */
break;
case SDL_POWERSTATE_CHARGED:
msg = "Charged.";
barg = 255; /* draw in green */
break;
}
SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255);
SDL_RenderClear(renderer);
if (percent >= 0) {
float x, y;
SDL_FRect pctrect;
char remainstr[64];
char msgbuf[128];
SDL_copyp(&pctrect, &frame);
pctrect.w *= percent / 100.0f;
if (seconds < 0) {
SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr));
} else {
int hours, minutes;
hours = seconds / (60 * 60);
seconds -= hours * (60 * 60);
minutes = seconds / 60;
seconds -= minutes * 60;
SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds);
}
SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr);
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f);
y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */
SDL_RenderFillRect(renderer, &pctrect);
SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */
SDL_RenderRect(renderer, &frame);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */
}
if (msg) {
const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msg);
}
if (msg2) {
const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f);
const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4);
SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255);
SDL_RenderDebugText(renderer, x, y, msg2);
}
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
/* SDL will clean up the window/renderer for us. */
}

View File

@@ -1,6 +1,6 @@
This example code lets the user copy and paste with the system clipboard.
This only handles text, but SDL supports other data types, too.
Note that only Chrome-based browsers support this API currently. This uses a
new Javascript API, so hopefully this will be available everywhere soon!
This example code lets the user copy and paste with the system clipboard.
This only handles text, but SDL supports other data types, too.
Note that only Chrome-based browsers support this API currently. This uses a
new Javascript API, so hopefully this will be available everywhere soon!

View File

@@ -1,235 +1,235 @@
/*
* This example code lets the user copy and paste with the system clipboard.
*
* This only handles text, but SDL supports other data types, too.
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static const char *copybuttonstr = "Click here to copy!";
static const char *pastebuttonstr = "Click here to paste!";
static SDL_FRect currenttimerect;
static SDL_FRect copybuttonrect;
static SDL_FRect pastetextrect;
static SDL_FRect pastebuttonrect;
static bool copy_pressed = false;
static bool paste_pressed = false;
static char current_time[64];
static char *pasted_str = NULL;
static void CalculateCurrentTimeString(void)
{
SDL_Time ticks = 0;
SDL_DateTime dt;
if (!SDL_GetCurrentTime(&ticks) || !SDL_TimeToDateTime(ticks, &dt, true)) {
SDL_snprintf(current_time, sizeof (current_time), "(Don't know the current time, sorry.)");
} else {
static const char *month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
static const char *day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
SDL_snprintf(current_time, sizeof (current_time), "%s, %s %d, %d %02d:%02d:%02d", day[dt.day_of_week], month[dt.month-1], dt.day, dt.year, dt.hour, dt.minute, dt.second);
}
}
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Clipboard", "1.0", "com.example.misc-clipboard");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/clipboard", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
CalculateCurrentTimeString();
/* set up the locations where we'll draw stuff. */
currenttimerect.x = 30;
currenttimerect.y = 10;
currenttimerect.w = 390;
currenttimerect.h = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 10;
copybuttonrect.x = currenttimerect.x + currenttimerect.w + 30;
copybuttonrect.y = currenttimerect.y;
copybuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(copybuttonstr)) + 10);
copybuttonrect.h = currenttimerect.h;
pastetextrect.x = 10;
pastetextrect.y = currenttimerect.y + currenttimerect.h + 10;
pastetextrect.w = 620;
pastetextrect.h = ((480 - pastetextrect.y) - copybuttonrect.h) - 20;
pastebuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(pastebuttonstr)) + 10);
pastebuttonrect.x = (640 - pastebuttonrect.w) / 2.0f;
pastebuttonrect.y = pastetextrect.y + pastetextrect.h + 10;
pastebuttonrect.h = copybuttonrect.h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
SDL_ConvertEventToRenderCoordinates(renderer, event);
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
if (event->button.button == SDL_BUTTON_LEFT) {
const SDL_FPoint p = { event->button.x, event->button.y };
copy_pressed = SDL_PointInRectFloat(&p, &copybuttonrect);
paste_pressed = SDL_PointInRectFloat(&p, &pastebuttonrect);
}
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
if (event->button.button == SDL_BUTTON_LEFT) {
const SDL_FPoint p = { event->button.x, event->button.y };
if (copy_pressed && SDL_PointInRectFloat(&p, &copybuttonrect)) {
SDL_SetClipboardText(current_time);
} else if (paste_pressed && SDL_PointInRectFloat(&p, &pastebuttonrect)) {
SDL_free(pasted_str);
pasted_str = SDL_GetClipboardText();
}
copy_pressed = paste_pressed = false;
}
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
static void RenderPastedText(void)
{
char *str = pasted_str;
if (str) {
float x = pastetextrect.x + 5;
float y = pastetextrect.y + 5;
const float w = pastetextrect.w - 10;
const float h = pastetextrect.h;
const size_t max_chars_per_line = (size_t) (w / SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE);
char *newline;
size_t slen;
char ch;
/* this doesn't wordwrap, or deal with Unicode....this is just a simple example app! */
while ((newline = SDL_strchr(str, '\n')) != NULL) {
const bool ignore_cr = ((newline > str) && (newline[-1] == '\r'));
if (ignore_cr) {
newline[-1] = '\0';
}
*newline = '\0';
slen = SDL_strlen(str); /* length to end of line. */
slen = SDL_min(slen, max_chars_per_line);
ch = str[slen];
str[slen] = '\0';
SDL_RenderDebugText(renderer, x, y, str);
str[slen] = ch;
if (ignore_cr) {
newline[-1] = '\r';
}
*newline = '\n';
str = newline + 1;
y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2);
if ((h - y) < SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
break; // no space for another line, stop here.
}
}
/* last text after newline, if there's room. */
if ((h - y) >= SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
slen = SDL_strlen(str); /* length to end of line. */
slen = SDL_min(slen, max_chars_per_line);
ch = str[slen];
str[slen] = '\0';
SDL_RenderDebugText(renderer, x, y, str);
str[slen] = ch;
}
}
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
float x, y;
CalculateCurrentTimeString();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black */
SDL_RenderClear(renderer);
/* draw a frame around the current time. */
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &currenttimerect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &currenttimerect);
/* draw the current time inside the frame. */
x = currenttimerect.x + ((currenttimerect.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(current_time))) / 2.0f);
y = currenttimerect.y + 5;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderDebugText(renderer, x, y, current_time);
/* draw a frame for the "copy the current time to the clipboard" button. */
if (copy_pressed) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &copybuttonrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &copybuttonrect);
/* draw the "copy this text" button string. */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, copybuttonrect.x + 5, copybuttonrect.y + 5, copybuttonstr);
/* draw a frame for the pasted text area. */
SDL_SetRenderDrawColor(renderer, 0, 53, 25, 255);
SDL_RenderFillRect(renderer, &pastetextrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &pastetextrect);
/* draw pasted text. */
SDL_SetRenderDrawColor(renderer, 0, 219, 107, 255);
RenderPastedText();
/* draw a frame for the "paste from the clipboard" button. */
if (paste_pressed) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &pastebuttonrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &pastebuttonrect);
/* draw the "paste some text" button string. */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, pastebuttonrect.x + 5, pastebuttonrect.y + 5, pastebuttonstr);
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
SDL_free(pasted_str);
/* SDL will clean up the window/renderer for us. */
}
/*
* This example code lets the user copy and paste with the system clipboard.
*
* This only handles text, but SDL supports other data types, too.
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static const char *copybuttonstr = "Click here to copy!";
static const char *pastebuttonstr = "Click here to paste!";
static SDL_FRect currenttimerect;
static SDL_FRect copybuttonrect;
static SDL_FRect pastetextrect;
static SDL_FRect pastebuttonrect;
static bool copy_pressed = false;
static bool paste_pressed = false;
static char current_time[64];
static char *pasted_str = NULL;
static void CalculateCurrentTimeString(void)
{
SDL_Time ticks = 0;
SDL_DateTime dt;
if (!SDL_GetCurrentTime(&ticks) || !SDL_TimeToDateTime(ticks, &dt, true)) {
SDL_snprintf(current_time, sizeof (current_time), "(Don't know the current time, sorry.)");
} else {
static const char *month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
static const char *day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
SDL_snprintf(current_time, sizeof (current_time), "%s, %s %d, %d %02d:%02d:%02d", day[dt.day_of_week], month[dt.month-1], dt.day, dt.year, dt.hour, dt.minute, dt.second);
}
}
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Clipboard", "1.0", "com.example.misc-clipboard");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/clipboard", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
CalculateCurrentTimeString();
/* set up the locations where we'll draw stuff. */
currenttimerect.x = 30;
currenttimerect.y = 10;
currenttimerect.w = 390;
currenttimerect.h = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 10;
copybuttonrect.x = currenttimerect.x + currenttimerect.w + 30;
copybuttonrect.y = currenttimerect.y;
copybuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(copybuttonstr)) + 10);
copybuttonrect.h = currenttimerect.h;
pastetextrect.x = 10;
pastetextrect.y = currenttimerect.y + currenttimerect.h + 10;
pastetextrect.w = 620;
pastetextrect.h = ((480 - pastetextrect.y) - copybuttonrect.h) - 20;
pastebuttonrect.w = (float) ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(pastebuttonstr)) + 10);
pastebuttonrect.x = (640 - pastebuttonrect.w) / 2.0f;
pastebuttonrect.y = pastetextrect.y + pastetextrect.h + 10;
pastebuttonrect.h = copybuttonrect.h;
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
SDL_ConvertEventToRenderCoordinates(renderer, event);
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
if (event->button.button == SDL_BUTTON_LEFT) {
const SDL_FPoint p = { event->button.x, event->button.y };
copy_pressed = SDL_PointInRectFloat(&p, &copybuttonrect);
paste_pressed = SDL_PointInRectFloat(&p, &pastebuttonrect);
}
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
if (event->button.button == SDL_BUTTON_LEFT) {
const SDL_FPoint p = { event->button.x, event->button.y };
if (copy_pressed && SDL_PointInRectFloat(&p, &copybuttonrect)) {
SDL_SetClipboardText(current_time);
} else if (paste_pressed && SDL_PointInRectFloat(&p, &pastebuttonrect)) {
SDL_free(pasted_str);
pasted_str = SDL_GetClipboardText();
}
copy_pressed = paste_pressed = false;
}
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
static void RenderPastedText(void)
{
char *str = pasted_str;
if (str) {
float x = pastetextrect.x + 5;
float y = pastetextrect.y + 5;
const float w = pastetextrect.w - 10;
const float h = pastetextrect.h;
const size_t max_chars_per_line = (size_t) (w / SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE);
char *newline;
size_t slen;
char ch;
/* this doesn't wordwrap, or deal with Unicode....this is just a simple example app! */
while ((newline = SDL_strchr(str, '\n')) != NULL) {
const bool ignore_cr = ((newline > str) && (newline[-1] == '\r'));
if (ignore_cr) {
newline[-1] = '\0';
}
*newline = '\0';
slen = SDL_strlen(str); /* length to end of line. */
slen = SDL_min(slen, max_chars_per_line);
ch = str[slen];
str[slen] = '\0';
SDL_RenderDebugText(renderer, x, y, str);
str[slen] = ch;
if (ignore_cr) {
newline[-1] = '\r';
}
*newline = '\n';
str = newline + 1;
y += (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2);
if ((h - y) < SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
break; // no space for another line, stop here.
}
}
/* last text after newline, if there's room. */
if ((h - y) >= SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) {
slen = SDL_strlen(str); /* length to end of line. */
slen = SDL_min(slen, max_chars_per_line);
ch = str[slen];
str[slen] = '\0';
SDL_RenderDebugText(renderer, x, y, str);
str[slen] = ch;
}
}
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
float x, y;
CalculateCurrentTimeString();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black */
SDL_RenderClear(renderer);
/* draw a frame around the current time. */
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &currenttimerect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &currenttimerect);
/* draw the current time inside the frame. */
x = currenttimerect.x + ((currenttimerect.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(current_time))) / 2.0f);
y = currenttimerect.y + 5;
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderDebugText(renderer, x, y, current_time);
/* draw a frame for the "copy the current time to the clipboard" button. */
if (copy_pressed) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &copybuttonrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &copybuttonrect);
/* draw the "copy this text" button string. */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, copybuttonrect.x + 5, copybuttonrect.y + 5, copybuttonstr);
/* draw a frame for the pasted text area. */
SDL_SetRenderDrawColor(renderer, 0, 53, 25, 255);
SDL_RenderFillRect(renderer, &pastetextrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &pastetextrect);
/* draw pasted text. */
SDL_SetRenderDrawColor(renderer, 0, 219, 107, 255);
RenderPastedText();
/* draw a frame for the "paste from the clipboard" button. */
if (paste_pressed) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &pastebuttonrect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderRect(renderer, &pastebuttonrect);
/* draw the "paste some text" button string. */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, pastebuttonrect.x + 5, pastebuttonrect.y + 5, pastebuttonstr);
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
SDL_free(pasted_str);
/* SDL will clean up the window/renderer for us. */
}

View File

@@ -1 +1 @@
This example code reports the currently selected locales.
This example code reports the currently selected locales.

View File

@@ -1,94 +1,94 @@
/*
* This example code reports the currently selected locales.
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Locale", "1.0", "com.example.misc-locale");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/locale", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const SDL_FRect frame = { 0, 0, 640, 480 };
SDL_Locale **locales;
char msg[128];
int count, i;
float x, y;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
locales = SDL_GetPreferredLocales(&count);
if (!locales) {
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
} else {
SDL_snprintf(msg, sizeof (msg), "Locales, in order of preference (%d total):", count);
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
for (i = 0; locales[i]; ++i) {
const SDL_Locale *l = locales[i];
const char *c = l->country;
SDL_snprintf(msg, sizeof (msg), " - %s%s%s", l->language, c ? "_" : "", c ? c : "");
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y + ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2) * (i + 1));
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
}
SDL_free(locales);
}
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
/* SDL will clean up the window/renderer for us. */
}
/*
* This example code reports the currently selected locales.
*
* This code is public domain. Feel free to use it for any purpose!
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
SDL_SetAppMetadata("Example Misc Locale", "1.0", "com.example.misc-locale");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/misc/locale", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const SDL_FRect frame = { 0, 0, 640, 480 };
SDL_Locale **locales;
char msg[128];
int count, i;
float x, y;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
locales = SDL_GetPreferredLocales(&count);
if (!locales) {
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
} else {
SDL_snprintf(msg, sizeof (msg), "Locales, in order of preference (%d total):", count);
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
for (i = 0; locales[i]; ++i) {
const SDL_Locale *l = locales[i];
const char *c = l->country;
SDL_snprintf(msg, sizeof (msg), " - %s%s%s", l->language, c ? "_" : "", c ? c : "");
x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
y = frame.y + ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2) * (i + 1));
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, msg);
}
SDL_free(locales);
}
/* put the new rendering on the screen. */
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
/* SDL will clean up the window/renderer for us. */
}