233 lines
6.3 KiB
C++
233 lines
6.3 KiB
C++
// #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
|
#include <SDL3/SDL.h>
|
|
#include <stdio.h>
|
|
#include <math.h>
|
|
#include <GLES3/gl3.h>
|
|
|
|
#if defined(__ANDROID__)
|
|
#define APP_MAIN SDL_main
|
|
#define SDL_WINDOW_FLAG SDL_WINDOW_FULLSCREEN
|
|
#else
|
|
#define APP_MAIN main
|
|
#define SDL_WINDOW_FLAG 0
|
|
#endif
|
|
|
|
#define SCREEN_WIDTH 1600
|
|
#define SCREEN_HEIGHT 720
|
|
|
|
#define BOX_WIDTH 100
|
|
#define BOX_HEIGHT 100
|
|
|
|
static SDL_Window *window = NULL;
|
|
|
|
static bool running = true;
|
|
|
|
static Uint8 color = 0x18;
|
|
|
|
static Uint64 elapsed = 0;
|
|
static float boxVelX = 0.5f;
|
|
static float boxVelY = 0.5f;
|
|
|
|
static float boxSpeed = 300.f;
|
|
|
|
const char* vShader = "#version 300 es\n"
|
|
"layout (location = 0) in vec2 aPos;\n"
|
|
"void main()\n"
|
|
"{\n"
|
|
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
|
|
"}";
|
|
|
|
const char* fShader = "#version 300 es\n"
|
|
"precision mediump float;\n"
|
|
"out vec4 fragColor;\n"
|
|
"void main()\n"
|
|
"{\n"
|
|
" fragColor = vec4(0.8, 0.8, 0.8, 1.0);\n"
|
|
"}";
|
|
|
|
void computeLetterbox(int windowW, int windowH,
|
|
int logicalW, int logicalH,
|
|
int* x, int* y, int* w, int* h)
|
|
{
|
|
float scaleX = (float)windowW / logicalW;
|
|
float scaleY = (float)windowH / logicalH;
|
|
|
|
float scale = (scaleX < scaleY) ? scaleX : scaleY;
|
|
|
|
*w = (int)(logicalW * scale);
|
|
*h = (int)(logicalH * scale);
|
|
|
|
*x = (windowW - *w) / 2;
|
|
*y = (windowH - *h) / 2;
|
|
}
|
|
|
|
struct Rect {
|
|
float x;
|
|
float y;
|
|
float w;
|
|
float h;
|
|
};
|
|
|
|
extern "C" int APP_MAIN(int argc, char **argv) {
|
|
SDL_SetAppMetadata("Example Renderer Primitives", "1.0", "com.example.renderer-primitives");
|
|
SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight");
|
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
|
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
|
|
window = SDL_CreateWindow("examples/renderer/primitives", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FLAG | SDL_WINDOW_OPENGL);
|
|
if (!window) {
|
|
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
|
|
|
int success;
|
|
char infoLog[512];
|
|
|
|
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vertexShader, 1, &vShader, NULL);
|
|
glCompileShader(vertexShader);
|
|
// print compile errors if any
|
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
|
if (!success)
|
|
{
|
|
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
|
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", infoLog);
|
|
};
|
|
|
|
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fragmentShader, 1, &fShader, NULL);
|
|
glCompileShader(fragmentShader);
|
|
// print compile errors if any
|
|
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
|
if (!success)
|
|
{
|
|
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
|
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", infoLog);
|
|
};
|
|
|
|
GLuint program = glCreateProgram();
|
|
glAttachShader(program, vertexShader);
|
|
glAttachShader(program, fragmentShader);
|
|
glLinkProgram(program);
|
|
// print linking errors if any
|
|
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
|
if (!success)
|
|
{
|
|
glGetProgramInfoLog(program, 512, NULL, infoLog);
|
|
printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s\n", infoLog);
|
|
}
|
|
|
|
glDeleteShader(vertexShader);
|
|
glDeleteShader(fragmentShader);
|
|
|
|
float vertices[12] = {0};
|
|
|
|
Rect rect = {
|
|
.x = 50.f,
|
|
.y = 50.f,
|
|
.w = BOX_WIDTH,
|
|
.h = BOX_HEIGHT,
|
|
};
|
|
|
|
GLuint vao, vbo;
|
|
glGenVertexArrays(1, &vao);
|
|
glGenBuffers(1, &vbo);
|
|
|
|
glBindVertexArray(vao);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindVertexArray(0);
|
|
|
|
int drawableW, drawableH;
|
|
SDL_GetWindowSizeInPixels(window, &drawableW, &drawableH);
|
|
|
|
#if defined(__ANDROID__)
|
|
int vx, vy, vw, vh;
|
|
computeLetterbox(drawableW, drawableH,
|
|
SCREEN_WIDTH, SCREEN_HEIGHT,
|
|
&vx, &vy, &vw, &vh);
|
|
|
|
glViewport(vx, vy, vw, vh);
|
|
#else
|
|
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
#endif
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
while (running) {
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
if (event.type == SDL_EVENT_QUIT) {
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
auto now = SDL_GetTicks();
|
|
float dt = now - elapsed;
|
|
elapsed = now;
|
|
// SDL_Log("dt: %f\n", dt);
|
|
dt /= 1000.f;
|
|
|
|
rect.x += boxSpeed * boxVelX * dt;
|
|
rect.y += boxSpeed * boxVelY * dt;
|
|
// boxSpeed += 50.f * dt;
|
|
|
|
if (rect.x + BOX_WIDTH >= SCREEN_WIDTH || rect.x <= 0) boxVelX *= -1.f;
|
|
if (rect.y + BOX_HEIGHT >= SCREEN_HEIGHT || rect.y <= 0) boxVelY *= -1.f;
|
|
|
|
// float xProj = -1 + rect.x / ((float)SCREEN_WIDTH / 2);
|
|
// float yProj = rect.y / ((float)SCREEN_HEIGHT / 2);
|
|
|
|
float xProj = (rect.x / SCREEN_WIDTH) * 2.0f - 1.0f;
|
|
float yProj = 1.0f - (rect.y / SCREEN_HEIGHT) * 2.0f;
|
|
|
|
float wProj = (rect.w / SCREEN_WIDTH) * 2.0f;
|
|
float hProj = (rect.h / SCREEN_HEIGHT) * 2.0f;
|
|
SDL_Log("%f::%f (%f:%f)\n", xProj, yProj, wProj, hProj);
|
|
|
|
vertices[0] = xProj;
|
|
vertices[1] = yProj;
|
|
vertices[2] = xProj + wProj;
|
|
vertices[3] = yProj;
|
|
vertices[4] = xProj + wProj;
|
|
vertices[5] = yProj - hProj;
|
|
|
|
vertices[6] = xProj;
|
|
vertices[7] = yProj;
|
|
vertices[8] = xProj;
|
|
vertices[9] = yProj - hProj;
|
|
vertices[10] = xProj + wProj;
|
|
vertices[11] = yProj - hProj;
|
|
|
|
glClearColor(0x18/255.f, 0x18/255.f, 0x18/255.f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// Draw your 3D stuff here
|
|
glBindVertexArray(vao);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
|
|
|
glUseProgram(program);
|
|
glBindVertexArray(vao);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
}
|
|
|
|
return 0;
|
|
}
|