mirror of
https://github.com/GayPizzaSpecifications/padlab.git
synced 2025-08-03 21:21:33 +00:00
refactors & viewport fix
This commit is contained in:
@ -1,27 +1,20 @@
|
|||||||
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
||||||
project(padlab C)
|
project(padlab C)
|
||||||
set(CMAKE_C_STANDARD 99)
|
|
||||||
set(TARGET padlab)
|
set(TARGET padlab)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
find_package(OpenGL)
|
find_package(OpenGL)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
maths.h
|
maths.h
|
||||||
draw.h
|
draw.h $<IF:$<BOOL:${OPENGL_FOUND}>,draw_opengl.c,draw.c>
|
||||||
stick.c stick.h
|
stick.c stick.h
|
||||||
analogue.c)
|
analogue.c)
|
||||||
|
|
||||||
if (OPENGL_FOUND)
|
|
||||||
list(APPEND SOURCES draw_opengl.c)
|
|
||||||
else()
|
|
||||||
list(APPEND SOURCES draw.c)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_executable(${TARGET} ${SOURCES})
|
add_executable(${TARGET} ${SOURCES})
|
||||||
target_link_libraries(${TARGET} SDL2::SDL2 m)
|
target_link_libraries(${TARGET}
|
||||||
if (OPENGL_FOUND)
|
SDL2::SDL2 $<$<BOOL:${OPENGL_FOUND}>:OpenGL::GL> m)
|
||||||
target_link_libraries(${TARGET} OpenGL::GL)
|
|
||||||
endif()
|
|
||||||
target_compile_options(${TARGET} PRIVATE
|
target_compile_options(${TARGET} PRIVATE
|
||||||
-Wall -Wextra -pedantic -Wno-unused-parameter)
|
-Wall -Wextra -pedantic -Wno-unused-parameter)
|
||||||
|
11
analogue.c
11
analogue.c
@ -9,11 +9,11 @@
|
|||||||
#define WINDOW_WIDTH 512
|
#define WINDOW_WIDTH 512
|
||||||
#define WINDOW_HEIGHT 288
|
#define WINDOW_HEIGHT 288
|
||||||
|
|
||||||
SDL_Window* window = NULL;
|
static SDL_Window* window = NULL;
|
||||||
SDL_JoystickID joyid = -1;
|
static SDL_JoystickID joyid = -1;
|
||||||
SDL_GameController* pad = NULL;
|
static SDL_GameController* pad = NULL;
|
||||||
|
|
||||||
bool UseGamepad(int aJoyid)
|
static bool UseGamepad(int aJoyid)
|
||||||
{
|
{
|
||||||
pad = SDL_GameControllerOpen(aJoyid);
|
pad = SDL_GameControllerOpen(aJoyid);
|
||||||
joyid = SDL_JoystickGetDeviceInstanceID(aJoyid);
|
joyid = SDL_JoystickGetDeviceInstanceID(aJoyid);
|
||||||
@ -121,6 +121,7 @@ int main(int argc, char** argv)
|
|||||||
winw = event.window.data1;
|
winw = event.window.data1;
|
||||||
winh = event.window.data2;
|
winh = event.window.data2;
|
||||||
rendSize = GetDrawSizeInPixels();
|
rendSize = GetDrawSizeInPixels();
|
||||||
|
SetDrawViewport(rendSize);
|
||||||
repaint = true;
|
repaint = true;
|
||||||
}
|
}
|
||||||
else if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
|
else if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
|
||||||
@ -218,7 +219,7 @@ int main(int argc, char** argv)
|
|||||||
if (repaint)
|
if (repaint)
|
||||||
{
|
{
|
||||||
// background
|
// background
|
||||||
SetDrawColour(0x1F1F1FFF);
|
SetDrawColour(MKGREY(0x1F, 0xFF));
|
||||||
DrawClear();
|
DrawClear();
|
||||||
|
|
||||||
const int hrw = rendSize.w / 2;
|
const int hrw = rendSize.w / 2;
|
||||||
|
2
draw.c
2
draw.c
@ -27,6 +27,8 @@ size GetDrawSizeInPixels(void)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetDrawViewport(size size) {}
|
||||||
|
|
||||||
|
|
||||||
void SetDrawColour(uint32_t c)
|
void SetDrawColour(uint32_t c)
|
||||||
{
|
{
|
||||||
|
5
draw.h
5
draw.h
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#define DISPLAY_SCALE 0.8889
|
#define DISPLAY_SCALE 0.8889
|
||||||
|
|
||||||
#include "maths.h"
|
#include "util.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct SDL_Window SDL_Window;
|
typedef struct SDL_Window SDL_Window;
|
||||||
@ -34,6 +34,9 @@ void QuitDraw(void);
|
|||||||
// height of the canvas in actual pixels.
|
// height of the canvas in actual pixels.
|
||||||
size GetDrawSizeInPixels(void);
|
size GetDrawSizeInPixels(void);
|
||||||
|
|
||||||
|
// Call on resize for backends that need manual viewport resizing.
|
||||||
|
void SetDrawViewport(size size);
|
||||||
|
|
||||||
// Set the current draw colour.
|
// Set the current draw colour.
|
||||||
//
|
//
|
||||||
// Params:
|
// Params:
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "draw.h"
|
#include "draw.h"
|
||||||
|
#include "maths.h"
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
#include <SDL_opengl.h>
|
#include <SDL_opengl.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -42,7 +43,7 @@ int InitDraw(SDL_Window* w)
|
|||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
glEnable(GL_MULTISAMPLE);
|
glEnable(GL_MULTISAMPLE);
|
||||||
|
|
||||||
GetDrawSizeInPixels(); // Fills scaleWidth & scaleHeight
|
SetDrawViewport(GetDrawSizeInPixels()); // Fills scaleWidth & scaleHeight
|
||||||
|
|
||||||
// Setup orthographic viewport
|
// Setup orthographic viewport
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
@ -65,11 +66,16 @@ size GetDrawSizeInPixels(void)
|
|||||||
{
|
{
|
||||||
size out;
|
size out;
|
||||||
SDL_GL_GetDrawableSize(SDL_GL_GetCurrentWindow(), &out.w, &out.h);
|
SDL_GL_GetDrawableSize(SDL_GL_GetCurrentWindow(), &out.w, &out.h);
|
||||||
scaleWidth = 1.0 / (double)out.w;
|
|
||||||
scaleHeight = 1.0 / (double)out.h;
|
|
||||||
return out;
|
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)
|
void SetDrawColour(uint32_t c)
|
||||||
{
|
{
|
||||||
|
7
maths.h
7
maths.h
@ -6,15 +6,8 @@
|
|||||||
#define PI 3.141592653589793238462643383279502884L
|
#define PI 3.141592653589793238462643383279502884L
|
||||||
#define TAU 6.283185307179586476925286766559005768L
|
#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 double vec_t;
|
||||||
typedef struct { vec_t x, y; } vector;
|
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)
|
static inline vector VecAdd(vector l, vector r)
|
||||||
{
|
{
|
||||||
|
22
stick.c
22
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;
|
double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
||||||
|
|
||||||
// range rect
|
// range rect
|
||||||
SetDrawColour(0x3F3F3FFF);
|
SetDrawColour(MKGREY(0x3F, 0xFF));
|
||||||
const int rectSz = (int)round(size);
|
const int rectSz = (int)round(size);
|
||||||
DrawRect(
|
DrawRect(
|
||||||
win->x + (win->w - rectSz) / 2,
|
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;
|
const int oy = win->y + win->h / 2;
|
||||||
|
|
||||||
// acceleration curve
|
// acceleration curve
|
||||||
SetDrawColour(0x4F4F4FFF);
|
SetDrawColour(MKGREY(0x4F, 0xFF));
|
||||||
const int accelsamp = (int)(sqrt(size) * 4.20);
|
const int accelsamp = (int)(sqrt(size) * 4.20);
|
||||||
const double step = 1.0 / (double)accelsamp;
|
const double step = 1.0 / (double)accelsamp;
|
||||||
double y1 = AccelCurve(0.0, p->accelpow);
|
double y1 = AccelCurve(0.0, p->accelpow);
|
||||||
@ -114,14 +114,14 @@ void DrawAnalogue(const rect* win, StickState* p)
|
|||||||
oy + tickery);
|
oy + tickery);
|
||||||
|
|
||||||
// guide circle
|
// guide circle
|
||||||
SetDrawColour(0x4F4F4FFF);
|
SetDrawColour(MKGREY(0x4F, 0xFF));
|
||||||
DrawCircle(ox, oy, (int)round(size) / 2);
|
DrawCircle(ox, oy, (int)round(size) / 2);
|
||||||
|
|
||||||
SetDrawColour(0x3F3F3FFF);
|
SetDrawColour(MKGREY(0x3F, 0xFF));
|
||||||
DrawCircle(ox, oy, (int)round(p->deadzone * size) / 2);
|
DrawCircle(ox, oy, (int)round(p->deadzone * size) / 2);
|
||||||
|
|
||||||
// 0,0 line axis'
|
// 0,0 line axis'
|
||||||
SetDrawColour(0x2F2F2FFF);
|
SetDrawColour(MKGREY(0x2F, 0xFF));
|
||||||
DrawLine(
|
DrawLine(
|
||||||
win->x, oy,
|
win->x, oy,
|
||||||
win->x + win->w, 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));
|
oy + (int)round(p->compos.y * size / 2.0));
|
||||||
|
|
||||||
// raw position
|
// raw position
|
||||||
SetDrawColour(0xFFFFFFFF);
|
SetDrawColour(WHITE);
|
||||||
DrawLine(
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0),
|
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;
|
const double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
||||||
|
|
||||||
// range rect
|
// range rect
|
||||||
SetDrawColour(0x3F3F3FFF);
|
SetDrawColour(MKGREY(0x3F, 0xFF));
|
||||||
const int rectSz = (int)round(size);
|
const int rectSz = (int)round(size);
|
||||||
DrawRect(
|
DrawRect(
|
||||||
win->x + (win->w - rectSz) / 2,
|
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;
|
const int oy = win->y + win->h / 2;
|
||||||
|
|
||||||
// guide circle
|
// guide circle
|
||||||
SetDrawColour(0x4F4F4FFF);
|
SetDrawColour(MKGREY(0x4F, 0xFF));
|
||||||
DrawCircle(ox, oy, (int)round(size) / 2);
|
DrawCircle(ox, oy, (int)round(size) / 2);
|
||||||
|
|
||||||
// 0,0 line axis'
|
// 0,0 line axis'
|
||||||
SetDrawColour(0x2F2F2FFF);
|
SetDrawColour(MKGREY(0x2F, 0xFF));
|
||||||
DrawLine(
|
DrawLine(
|
||||||
win->x, oy,
|
win->x, oy,
|
||||||
win->x + win->w, 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 innh = (int)round(p->digideadzone * size / 2.0);
|
||||||
const int innq = (int)round(p->digideadzone * size / 2.0 * p->digiangle);
|
const int innq = (int)round(p->digideadzone * size / 2.0 * p->digiangle);
|
||||||
|
|
||||||
SetDrawColour(0x3F3F3FFF);
|
SetDrawColour(MKGREY(0x3F, 0xFF));
|
||||||
|
|
||||||
// angles preview
|
// angles preview
|
||||||
DrawLine(ox - outq, oy - outh, ox - innq, oy - innh);
|
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));
|
oy + (int)round(p->compos.y * size / 2.0));
|
||||||
|
|
||||||
// raw position
|
// raw position
|
||||||
SetDrawColour(0xFFFFFFFF);
|
SetDrawColour(WHITE);
|
||||||
DrawLine(
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0),
|
oy + (int)round(p->rawpos.y * size / 2.0),
|
||||||
|
1
stick.h
1
stick.h
@ -2,6 +2,7 @@
|
|||||||
#define STICK_H
|
#define STICK_H
|
||||||
|
|
||||||
#include "maths.h"
|
#include "maths.h"
|
||||||
|
#include "util.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
22
util.h
Normal file
22
util.h
Normal 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
|
Reference in New Issue
Block a user