refactors & viewport fix

This commit is contained in:
2022-11-16 09:20:44 +11:00
parent 9ba7a3bda8
commit a4cd5094b1
9 changed files with 61 additions and 40 deletions

View File

@ -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 $<IF:$<BOOL:${OPENGL_FOUND}>,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 $<$<BOOL:${OPENGL_FOUND}>:OpenGL::GL> m)
target_compile_options(${TARGET} PRIVATE
-Wall -Wextra -pedantic -Wno-unused-parameter)

View File

@ -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;

2
draw.c
View File

@ -27,6 +27,8 @@ size GetDrawSizeInPixels(void)
return out;
}
void SetDrawViewport(size size) {}
void SetDrawColour(uint32_t c)
{

5
draw.h
View File

@ -3,7 +3,7 @@
#define DISPLAY_SCALE 0.8889
#include "maths.h"
#include "util.h"
#include <stdint.h>
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:

View File

@ -1,4 +1,5 @@
#include "draw.h"
#include "maths.h"
#include <SDL_video.h>
#include <SDL_opengl.h>
#include <stdbool.h>
@ -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)
{

View File

@ -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)
{

22
stick.c
View File

@ -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),

View File

@ -2,6 +2,7 @@
#define STICK_H
#include "maths.h"
#include "util.h"
#include <stdbool.h>
typedef struct

22
util.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdint.h>
#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