diff --git a/CMakeLists.txt b/CMakeLists.txt index 50ad91c..ec00483 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,27 +1,20 @@ -cmake_minimum_required(VERSION 3.1 FATAL_ERROR) +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) project(padlab C) -set(CMAKE_C_STANDARD 99) set(TARGET padlab) +set(CMAKE_C_STANDARD 99) + find_package(SDL2 REQUIRED) find_package(OpenGL) set(SOURCES maths.h - draw.h + draw.h $,draw_opengl.c,draw.c> stick.c stick.h analogue.c) -if (OPENGL_FOUND) - list(APPEND SOURCES draw_opengl.c) -else() - list(APPEND SOURCES draw.c) -endif() - add_executable(${TARGET} ${SOURCES}) -target_link_libraries(${TARGET} SDL2::SDL2 m) -if (OPENGL_FOUND) - target_link_libraries(${TARGET} OpenGL::GL) -endif() +target_link_libraries(${TARGET} + SDL2::SDL2 $<$:OpenGL::GL> m) target_compile_options(${TARGET} PRIVATE -Wall -Wextra -pedantic -Wno-unused-parameter) diff --git a/analogue.c b/analogue.c index 370c11f..06126c4 100644 --- a/analogue.c +++ b/analogue.c @@ -9,11 +9,11 @@ #define WINDOW_WIDTH 512 #define WINDOW_HEIGHT 288 -SDL_Window* window = NULL; -SDL_JoystickID joyid = -1; -SDL_GameController* pad = NULL; +static SDL_Window* window = NULL; +static SDL_JoystickID joyid = -1; +static SDL_GameController* pad = NULL; -bool UseGamepad(int aJoyid) +static bool UseGamepad(int aJoyid) { pad = SDL_GameControllerOpen(aJoyid); joyid = SDL_JoystickGetDeviceInstanceID(aJoyid); @@ -121,6 +121,7 @@ int main(int argc, char** argv) winw = event.window.data1; winh = event.window.data2; rendSize = GetDrawSizeInPixels(); + SetDrawViewport(rendSize); repaint = true; } else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) @@ -218,7 +219,7 @@ int main(int argc, char** argv) if (repaint) { // background - SetDrawColour(0x1F1F1FFF); + SetDrawColour(MKGREY(0x1F, 0xFF)); DrawClear(); const int hrw = rendSize.w / 2; diff --git a/draw.c b/draw.c index 615f163..c579ba1 100644 --- a/draw.c +++ b/draw.c @@ -27,6 +27,8 @@ size GetDrawSizeInPixels(void) return out; } +void SetDrawViewport(size size) {} + void SetDrawColour(uint32_t c) { diff --git a/draw.h b/draw.h index 97c5251..c53b136 100644 --- a/draw.h +++ b/draw.h @@ -3,7 +3,7 @@ #define DISPLAY_SCALE 0.8889 -#include "maths.h" +#include "util.h" #include typedef struct SDL_Window SDL_Window; @@ -34,6 +34,9 @@ void QuitDraw(void); // height of the canvas in actual pixels. size GetDrawSizeInPixels(void); +// Call on resize for backends that need manual viewport resizing. +void SetDrawViewport(size size); + // Set the current draw colour. // // Params: diff --git a/draw_opengl.c b/draw_opengl.c index 3eccf96..b70e22b 100644 --- a/draw_opengl.c +++ b/draw_opengl.c @@ -1,4 +1,5 @@ #include "draw.h" +#include "maths.h" #include #include #include @@ -42,7 +43,7 @@ int InitDraw(SDL_Window* w) glDisable(GL_CULL_FACE); glEnable(GL_MULTISAMPLE); - GetDrawSizeInPixels(); // Fills scaleWidth & scaleHeight + SetDrawViewport(GetDrawSizeInPixels()); // Fills scaleWidth & scaleHeight // Setup orthographic viewport glMatrixMode(GL_PROJECTION); @@ -65,11 +66,16 @@ size GetDrawSizeInPixels(void) { size out; SDL_GL_GetDrawableSize(SDL_GL_GetCurrentWindow(), &out.w, &out.h); - scaleWidth = 1.0 / (double)out.w; - scaleHeight = 1.0 / (double)out.h; return out; } +void SetDrawViewport(size size) +{ + glViewport(0, 0, size.w, size.h); + scaleWidth = 1.0 / (double)size.w; + scaleHeight = 1.0 / (double)size.h; +} + void SetDrawColour(uint32_t c) { diff --git a/maths.h b/maths.h index 5bf4860..589a5ff 100644 --- a/maths.h +++ b/maths.h @@ -6,15 +6,8 @@ #define PI 3.141592653589793238462643383279502884L #define TAU 6.283185307179586476925286766559005768L -#define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define MIN(A, B) ((A) < (B) ? (A) : (B)) -#define CLAMP(X, A, B) (MIN((B), MAX((A), (X)))) -#define SATURATE(X) (CLAMP((X), 0, 1)) - typedef double vec_t; typedef struct { vec_t x, y; } vector; -typedef struct { int w, h; } size; -typedef struct { int x, y, w, h; } rect; static inline vector VecAdd(vector l, vector r) { diff --git a/stick.c b/stick.c index 3263d40..5e2fd56 100644 --- a/stick.c +++ b/stick.c @@ -73,7 +73,7 @@ void DrawAnalogue(const rect* win, StickState* p) double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE; // range rect - SetDrawColour(0x3F3F3FFF); + SetDrawColour(MKGREY(0x3F, 0xFF)); const int rectSz = (int)round(size); DrawRect( win->x + (win->w - rectSz) / 2, @@ -84,7 +84,7 @@ void DrawAnalogue(const rect* win, StickState* p) const int oy = win->y + win->h / 2; // acceleration curve - SetDrawColour(0x4F4F4FFF); + SetDrawColour(MKGREY(0x4F, 0xFF)); const int accelsamp = (int)(sqrt(size) * 4.20); const double step = 1.0 / (double)accelsamp; double y1 = AccelCurve(0.0, p->accelpow); @@ -114,14 +114,14 @@ void DrawAnalogue(const rect* win, StickState* p) oy + tickery); // guide circle - SetDrawColour(0x4F4F4FFF); + SetDrawColour(MKGREY(0x4F, 0xFF)); DrawCircle(ox, oy, (int)round(size) / 2); - SetDrawColour(0x3F3F3FFF); + SetDrawColour(MKGREY(0x3F, 0xFF)); DrawCircle(ox, oy, (int)round(p->deadzone * size) / 2); // 0,0 line axis' - SetDrawColour(0x2F2F2FFF); + SetDrawColour(MKGREY(0x2F, 0xFF)); DrawLine( win->x, oy, win->x + win->w, oy); @@ -140,7 +140,7 @@ void DrawAnalogue(const rect* win, StickState* p) oy + (int)round(p->compos.y * size / 2.0)); // raw position - SetDrawColour(0xFFFFFFFF); + SetDrawColour(WHITE); DrawLine( ox + (int)round(p->rawpos.x * size / 2.0) - 4, oy + (int)round(p->rawpos.y * size / 2.0), @@ -164,7 +164,7 @@ void DrawDigital(const rect* win, StickState* p) const double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE; // range rect - SetDrawColour(0x3F3F3FFF); + SetDrawColour(MKGREY(0x3F, 0xFF)); const int rectSz = (int)round(size); DrawRect( win->x + (win->w - rectSz) / 2, @@ -176,11 +176,11 @@ void DrawDigital(const rect* win, StickState* p) const int oy = win->y + win->h / 2; // guide circle - SetDrawColour(0x4F4F4FFF); + SetDrawColour(MKGREY(0x4F, 0xFF)); DrawCircle(ox, oy, (int)round(size) / 2); // 0,0 line axis' - SetDrawColour(0x2F2F2FFF); + SetDrawColour(MKGREY(0x2F, 0xFF)); DrawLine( win->x, oy, win->x + win->w, oy); @@ -195,7 +195,7 @@ void DrawDigital(const rect* win, StickState* p) const int innh = (int)round(p->digideadzone * size / 2.0); const int innq = (int)round(p->digideadzone * size / 2.0 * p->digiangle); - SetDrawColour(0x3F3F3FFF); + SetDrawColour(MKGREY(0x3F, 0xFF)); // angles preview DrawLine(ox - outq, oy - outh, ox - innq, oy - innh); @@ -229,7 +229,7 @@ void DrawDigital(const rect* win, StickState* p) oy + (int)round(p->compos.y * size / 2.0)); // raw position - SetDrawColour(0xFFFFFFFF); + SetDrawColour(WHITE); DrawLine( ox + (int)round(p->rawpos.x * size / 2.0) - 4, oy + (int)round(p->rawpos.y * size / 2.0), diff --git a/stick.h b/stick.h index 54a4871..6d52d8b 100644 --- a/stick.h +++ b/stick.h @@ -2,6 +2,7 @@ #define STICK_H #include "maths.h" +#include "util.h" #include typedef struct diff --git a/util.h b/util.h new file mode 100644 index 0000000..8e1f3f3 --- /dev/null +++ b/util.h @@ -0,0 +1,22 @@ +#ifndef UTIL_H +#define UTIL_H + +#include + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define CLAMP(X, A, B) (MIN((B), MAX((A), (X)))) +#define SATURATE(X) (CLAMP((X), 0, 1)) + +typedef struct { int w, h; } size; +typedef struct { int x, y, w, h; } rect; + +#define MKGREY(L, A) (uint32_t)( \ + (((L) << 24) & 0xFF000000) | \ + (((L) << 16) & 0x00FF0000) | \ + (((L) << 8) & 0x0000FF00) | \ + ((A) & 0x000000FF)) + +#define WHITE 0xFFFFFFFF + +#endif//UTIL_H