Skip to content

Commit

Permalink
Added an initial skeleton program.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Sep 17, 2023
0 parents commit 859b722
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build

on: [push, pull_request]

jobs:
Build:
name: ${{ matrix.platform.name }}
runs-on: ${{ matrix.platform.os }}
strategy:
matrix:
platform:
- { name: Linux, os: ubuntu-latest }
- { name: Windows, os: windows-latest }
- { name: MacOS, os: macos-latest }
- { name: UWP, os: windows-latest }
- name: Set up SDL
id: sdl
uses: libsdl-org/setup-sdl@v1
with:
version: sdl3-head
- name: Get project sources
uses: actions/checkout@v3
- name: Configure CMake
run: cmake -B build ${{ matrix.platform.flags }}
- name: Build
run: cmake --build build/
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.12.0)
project(SDL_helloworld)

find_package(SDL3 CONFIG REQUIRED)

# WinRT/UWP/WindowsPhone needs a little more magic.
# If you never care about this target, you can delete this block and window-uwp-main.cpp, too.
if(WINDOWS_STORE)
target_compile_definitions(sdl-helloworld PRIVATE "SDL_MAIN_NOIMPL")
set_property(TARGET sdl-helloworld PROPERTY WIN32_EXECUTABLE TRUE)
add_library(main_uwp OBJECT windows-uwp-main.cpp)
target_link_libraries(main_uwp PRIVATE SDL3::Headers)
target_compile_options(main_uwp PRIVATE "/ZW")
set(EXTRA_LIBRARIES main_uwp)
endif()

add_executable(sdl-helloworld
main.c
)

target_link_libraries(sdl-helloworld PRIVATE SDL3::SDL3-shared)
target_link_libraries(sdl-helloworld PUBLIC SDL3::Headers ${EXTRA_LIBRARIES})

7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
All code in this sample is public domain. Do whatever you want with it.

We recommend you replace this text with your own license, unless you also
want your code to be public domain.



7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SDL_helloworld

This is a simple skeleton program that builds an extremely simple SDL-based
program, setting up CI for GitHub Actions and such.

Feel free to use this as a starting point for your own code.

100 changes: 100 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;

static int setup_program(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
SDL_Log("SDL_Init(SDL_INIT_VIDEO) failed: %s", SDL_GetError());
return -1;
}

window = SDL_CreateWindow("Hello SDL", 640, 480, 0);
if (!window) {
SDL_Log("SDL_CreateWindow() failed: %s", SDL_GetError());
return -1;
}

renderer = SDL_CreateRenderer(window, NULL, 0);
if (!renderer) {
SDL_Log("SDL_CreateRenderer() failed: %s", SDL_GetError());
return -1;
}

return 0;
}

static void mainloop(void)
{
SDL_FRect mouseposrect;
Uint8 r;
SDL_bool keep_going = SDL_TRUE;
SDL_Event event;

mouseposrect.x = mouseposrect.y = -1000; /* -1000 so it's offscreen at start */
mouseposrect.w = mouseposrect.h = 50;

/* run the program until told to stop. */
while (keep_going) {

/* run through all pending events until we run out. */
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: /* triggers on last window close and other things. End the program. */
keep_going = SDL_FALSE;
break;

case SDL_EVENT_KEY_DOWN: /* quit if user hits ESC key */
if (event.key.keysym.sym == SDLK_ESCAPE) {
keep_going = SDL_FALSE;
}
break;

case SDL_EVENT_MOUSE_MOTION: /* keep track of the latest mouse position */
/* center the square where the mouse is */
mouseposrect.x = event.motion.x - (mouseposrect.w / 2);
mouseposrect.y = event.motion.y - (mouseposrect.h / 2);
break;
}
}

/* fade between shades of red every 3 seconds, from 0 to 255. */
r = (Uint8) ((((float) (SDL_GetTicks() % 3000)) / 3000.0f) * 255.0f);
SDL_SetRenderDrawColor(renderer, r, 0, 0, 255);

/* you have to draw the whole window every frame. Clearing it makes sure the whole thing is sane. */
SDL_RenderClear(renderer); /* clear whole window to that fade color. */

/* set the color to white */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

/* draw a square where the mouse cursor currently is. */
SDL_RenderFillRect(renderer, &mouseposrect);

/* put everything we drew to the screen. */
SDL_RenderPresent(renderer);
}
}

static void shutdown_program(void)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}


/* this is always a main() function, even if you're on a platform that uses
something else. SDL_main.h takes care of figuring out the details and
making sure the program starts here. */
int main(int argc, char **argv)
{
if (setup_program(argc, argv) == 0) {
mainloop();
}
shutdown_program();
return 0;
}

12 changes: 12 additions & 0 deletions windows-uwp-main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* For Windows UWP/Windows Phone/WinRT, you need to have your entry point
* function in a .cpp file. If you're not building for this platform,
* (and you probably aren't) don't bother with this file.
*
* The CMakeLists.txt file knows to only compile this for the one
* platform, so this can just sit here and be ignored.
*/

/* Sets up WinMain and stuff, which we're doing here in a C++ file. */
#include <SDL3/SDL_main.h>

0 comments on commit 859b722

Please sign in to comment.