MVP for a Pong game

This commit is contained in:
Hartmut Seichter 2023-07-01 22:17:41 +02:00
parent 14343e7fd0
commit e9d0de8cdd
17 changed files with 517 additions and 109 deletions

View file

@ -1,16 +1,9 @@
set(paradiso_src
main.cpp
)
add_executable(
paradiso
${paradiso_src}
paradiso_simple
simple.cpp
)
target_link_libraries(paradiso
target_link_libraries(paradiso_simple
paradiso_core
)
target_include_directories(paradiso
PRIVATE
lib)

View file

@ -16,66 +16,6 @@
#include <iomanip>
#include <iostream>
void setup_shaders(paradiso::Shader& shader) {
const auto unlit_v = R"(
#version 400 core
layout (location = 0) in vec3 vertices;
layout (location = 1) in vec3 normals;
layout (location = 2) in vec2 texture_coords;
// pivot der sprite
uniform vec2 pivot = vec2( 0.0, 0.0 );
// scale
uniform vec2 scale = vec2( 1.0, 1.0 );
// rotation
uniform float rotation = 0.2;
// wir sind natuerlich in homogenenen 3D Koordinaten unterwegs
mat4 mm = mat4(
vec4( scale.x, 0.0, 0.0, 0.0),
vec4( 0.0, scale.y, 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( pivot, 0.0, 1.0)
);
float sir = sin(rotation);
float cor = cos(rotation);
mat4 mr = mat4(
vec4( cor, sir, 0.0, 0.0),
vec4(-sir, cor, 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( 0.0, 0.0, 0.0, 1.0)
);
out vec2 tex_c; // das hier reicht die texturkoordinaten durch
void main() {
tex_c = texture_coords; // umstaendlich aber notwendig
gl_Position = mm * mr * vec4(vertices, 1.0); // unsere eigentliche shader position
}
)";
const auto unlit_f = R"(
#version 400 core
uniform sampler2D tex_color; // hier ist unsere sprite textur (bitmap)
in vec2 tex_c; // da sind die texturkoordinaten wieder
out vec4 frag_color; // das hier wird der output (pixelwert/fragment)
void main() {
frag_color = texture(tex_color,tex_c);
})";
shader.set_source(paradiso::Shader::Type::Vertex, unlit_v);
shader.set_source(paradiso::Shader::Type::Fragment, unlit_f);
shader.build();
}
auto main() -> int {
// Ausgabefenster ... sieht aus als wäre es auf dem Stack
@ -85,7 +25,7 @@ auto main() -> int {
window
.set_size(paradiso::Size{.width = 1280, .height = 720}) // ... Grösse
.set_position(paradiso::Point{.x = 100, .y = 100}) // ... Position
.set_title("PardiSO") // ... Titel
.set_title("PardiSO.Simple") // ... Titel
.set_visible(true); // ... und jetzt anzeigen!
// der Fenster Kontext
@ -107,36 +47,39 @@ auto main() -> int {
// ein Shader (Schattierungsprogramm)
auto shader = paradiso::Shader{};
// hier werden die Shader Programme geladen, kompiliert usw.
setup_shaders(shader);
// wir nutzen einen vorgefertigten shader
shader.load_preset(paradiso::Shader::Preset::Sprite);
// kein schönes Design: dies sind globale Variablen ...
uint8_t slider_value = 0xFF;
bool want_close{false};
// eine sehr rudimentäre Eingabebehandlung. Bei vorhandenen
// Eingaben landen diese hier. Wer sich am Design beteiligen
// möchte: hier gibt es viel Potential zur Verbesserung ;)
window.set_keyboardcallback(
[&](auto& w, int key, int scancode, int action, int mods) {
if (key == 'Q' || key == 256) // Q oder ESC beenden das Programm
// hier "vor-deklariert" der Input-handler für unser Demo
auto SimpleKeyboardHandler =
[&](const paradiso::Window::KeyboardInputStack& input) {
// ohne Input kein Handling ...
if (input.empty())
return;
if (input.top().key == 'Q' ||
input.top().key == 256) // Q oder ESC beenden das Programm
want_close = true;
else if (key == 'B') { // kleine Spielerei
else if (input.top().key == 'B') { // kleine Spielerei
slider_value += 10;
} else if (key == 'W') {
} else if (input.top().key == 'W') {
sprite.pivot.y() += 0.1f;
} else if (key == 'S') {
} else if (input.top().key == 'S') {
sprite.pivot.y() -= 0.1f;
} else if (key == 'A') {
} else if (input.top().key == 'A') {
sprite.pivot.x() -= 0.1f;
} else if (key == 'D') {
} else if (input.top().key == 'D') {
sprite.pivot.x() += 0.1f;
} else if (key == 'P') {
} else if (input.top().key == 'P') {
sprite.scale *= 0.9;
} else if (key == 'R') {
} else if (input.top().key == 'R') {
sprite.rotation += 0.1;
}
});
};
// das update führt den hier mitgegebnen Ausdruck innerhalb der internen
// Updates des Fensters auf. Es wird hier auch explizit ein bool gefordert
@ -152,11 +95,15 @@ auto main() -> int {
// Pixeln sollte hier auch eine dementsprechende Grösse eingestellt
// werden
ctx.set_viewport(paradiso::Rectangle{
.position = paradiso::Point{.x = 0, .y = 0},
.size =
w.client_size().maximal_extent() // wir wollen das
// Seitenverhältnis beibehalten
});
// handle keyboard input ...
SimpleKeyboardHandler(w.keyboard_input());
// hier wird das eigentliche löschen des vorherigen Inhalts ausgelöst
ctx.clear();