refactored example by Robin
|
@ -1,3 +1,3 @@
|
|||
add_subdirectory(simple)
|
||||
add_subdirectory(pong)
|
||||
add_subdirectory(flappy_bird)
|
||||
add_subdirectory(quickwings)
|
|
@ -1,11 +0,0 @@
|
|||
file(GLOB_RECURSE flappy_bird_src "*.cpp")
|
||||
file(GLOB_RECURSE flappy_bird_lib "lib/*.hpp" "lib/*.h")
|
||||
file(GLOB_RECURSE flappy_bird_assets "assets/*")
|
||||
|
||||
add_executable(flappy_bird ${flappy_bird_src} ${flappy_bird_lib})
|
||||
target_link_libraries(flappy_bird paradiso_core)
|
||||
|
||||
add_custom_command(TARGET flappy_bird POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/assets/ $<TARGET_FILE_DIR:flappy_bird>/assets)
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
#include <paradiso/rgba.hpp>
|
||||
#include <paradiso/sprite.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
// STB image loading
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "lib/stb_image.h"
|
||||
|
||||
#include "lib/image_loader.hpp"
|
||||
|
||||
std::unordered_map<std::string, paradiso::Bitmap> image_loader::image_cache =
|
||||
std::unordered_map<std::string, paradiso::Bitmap>();
|
||||
|
||||
paradiso::Bitmap image_loader::load(const std::string& filename) {
|
||||
if (image_cache.find(filename) == image_cache.end()) {
|
||||
std::string path = ASSET_PATH + filename;
|
||||
std::cout << "Loading " << path << std::endl;
|
||||
auto data = readBMP(path);
|
||||
image_cache[filename] = data;
|
||||
}
|
||||
return image_cache[filename];
|
||||
}
|
||||
|
||||
paradiso::Bitmap image_loader::readBMP(std::string& filename) {
|
||||
// Load with stb_image
|
||||
stbi_set_flip_vertically_on_load(true); // flip y axis for OpenGL
|
||||
int width, height, channels;
|
||||
unsigned char* image =
|
||||
stbi_load(filename.c_str(), &width, &height, &channels, 4);
|
||||
int size = width * height;
|
||||
|
||||
if (!image)
|
||||
throw std::runtime_error("Error loading image");
|
||||
|
||||
// Convert to Vector of RGBA
|
||||
std::vector<paradiso::RGBA> rgba = std::vector<paradiso::RGBA>(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
// get rgba values
|
||||
int pos = i * 4;
|
||||
int r = image[pos + 0];
|
||||
int g = image[pos + 1];
|
||||
int b = image[pos + 2];
|
||||
int a = image[pos + 3];
|
||||
|
||||
// bug in from_rgba. it expects bgra, not rgba
|
||||
auto val = paradiso::RGBA::from_rgba(b, g, r, a);
|
||||
rgba[i] = val;
|
||||
}
|
||||
|
||||
// rotate image by 90 degrees CW
|
||||
std::vector<paradiso::RGBA> rotated = std::vector<paradiso::RGBA>(size);
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
|
||||
int pos = y * width + x;
|
||||
int pos_rotated = x * height + (height - y - 1);
|
||||
rotated[pos_rotated] = rgba[pos];
|
||||
}
|
||||
}
|
||||
|
||||
// free image
|
||||
stbi_image_free(image);
|
||||
|
||||
return paradiso::Bitmap::from_data(
|
||||
paradiso::Size{static_cast<unsigned int>(height),
|
||||
static_cast<unsigned int>(width)},
|
||||
rotated);
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <paradiso/bitmap.hpp>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class image_loader {
|
||||
public:
|
||||
/**
|
||||
* @brief loads an Image from a file
|
||||
* @param[in] filename path to file
|
||||
* @return bitmap from file
|
||||
*/
|
||||
static paradiso::Bitmap load(const std::string& filename);
|
||||
|
||||
private:
|
||||
static inline const std::string ASSET_PATH = "assets/";
|
||||
|
||||
static std::unordered_map<std::string, paradiso::Bitmap> image_cache;
|
||||
|
||||
static paradiso::Bitmap readBMP(std::string& filename);
|
||||
};
|
20
examples/quickwings/CMakeLists.txt
Normal file
|
@ -0,0 +1,20 @@
|
|||
set(quickwings_srcs quickwings.cpp)
|
||||
set(quickwings_assets
|
||||
assets/background-day.png
|
||||
assets/base.png
|
||||
assets/pipe-green.png
|
||||
assets/yellowbird-downflap.png
|
||||
assets/yellowbird-midflap.png
|
||||
assets/yellowbird-upflap.png
|
||||
)
|
||||
|
||||
set_source_files_properties(${quickwings_assets} PROPERTIES HEADER_FILE_ONLY TRUE)
|
||||
|
||||
add_executable(quickwings ${quickwings_srcs} ${quickwings_assets})
|
||||
|
||||
target_link_libraries(quickwings paradiso_core)
|
||||
|
||||
add_custom_command(TARGET quickwings POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/assets/ $<TARGET_FILE_DIR:quickwings>/assets)
|
||||
|
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 470 B After Width: | Height: | Size: 470 B |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 426 B After Width: | Height: | Size: 426 B |
Before Width: | Height: | Size: 425 B After Width: | Height: | Size: 425 B |
Before Width: | Height: | Size: 427 B After Width: | Height: | Size: 427 B |
|
@ -1,11 +1,12 @@
|
|||
/**
|
||||
* paradiso - Paradigmen der Softwareentwicklung
|
||||
*
|
||||
* (c) Copyright 2023 Hartmut Seichter
|
||||
* (c) Copyright 2023 Hartmut Seichter, Robin Rottstädt
|
||||
*
|
||||
*/
|
||||
|
||||
#include <paradiso/bitmap.hpp>
|
||||
#include <paradiso/bitmap_io.hpp>
|
||||
#include <paradiso/context.hpp>
|
||||
#include <paradiso/geometry.hpp>
|
||||
#include <paradiso/renderer.hpp>
|
||||
|
@ -17,9 +18,10 @@
|
|||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "lib/image_loader.hpp"
|
||||
// #include "lib/image_loader.hpp"
|
||||
|
||||
const int frame_rate = 60;
|
||||
|
||||
|
@ -33,7 +35,7 @@ struct Background {
|
|||
|
||||
Background() {
|
||||
auto backgroundImage =
|
||||
image_loader::load(std::string("background-day.png"));
|
||||
paradiso::BitmapIO::get().load("background-day.png");
|
||||
backgroundLeft = paradiso::Sprite{
|
||||
.bitmap = backgroundImage,
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, 0.0f),
|
||||
|
@ -43,7 +45,7 @@ struct Background {
|
|||
.pivot = paradiso::Vector2<float>::make(2.0f, 0.0f),
|
||||
.scale = paradiso::Vector2<float>::make(1.01f, 1.0f)};
|
||||
|
||||
auto grassImage = image_loader::load(std::string("base.png"));
|
||||
auto grassImage = paradiso::BitmapIO::get().load("base.png");
|
||||
grassLeft = paradiso::Sprite{
|
||||
.bitmap = grassImage,
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, -1.0f),
|
||||
|
@ -77,7 +79,7 @@ struct Grass {
|
|||
paradiso::Sprite* scrolling[2] = {&grassLeft, &grassRight};
|
||||
|
||||
Grass() {
|
||||
auto grassImage = image_loader::load(std::string("base.png"));
|
||||
auto grassImage = paradiso::BitmapIO::get().load(std::string("base.png"));
|
||||
grassLeft = paradiso::Sprite{
|
||||
.bitmap = grassImage,
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, -0.9f),
|
||||
|
@ -104,7 +106,7 @@ struct Grass {
|
|||
paradiso::Renderer renderer{};
|
||||
};
|
||||
|
||||
struct FlappyBird {
|
||||
struct QuickWings {
|
||||
|
||||
paradiso::Renderer renderer1{};
|
||||
paradiso::Renderer renderer2{};
|
||||
|
@ -131,24 +133,24 @@ struct FlappyBird {
|
|||
|
||||
float rotation = 0.0f;
|
||||
|
||||
FlappyBird() {
|
||||
QuickWings() {
|
||||
float scaleh = 0.07f;
|
||||
float scalew = scaleh * 1.416666666666667f;
|
||||
|
||||
birds = {
|
||||
paradiso::Sprite{
|
||||
.bitmap =
|
||||
image_loader::load(std::string("yellowbird-downflap.png")),
|
||||
paradiso::BitmapIO::get().load("yellowbird-downflap.png"),
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, 0.0f),
|
||||
.scale = paradiso::Vector2<float>::make(scalew, scaleh)},
|
||||
paradiso::Sprite{
|
||||
.bitmap =
|
||||
image_loader::load(std::string("yellowbird-midflap.png")),
|
||||
paradiso::BitmapIO::get().load("yellowbird-midflap.png"),
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, 0.0f),
|
||||
.scale = paradiso::Vector2<float>::make(scalew, scaleh)},
|
||||
paradiso::Sprite{
|
||||
.bitmap =
|
||||
image_loader::load(std::string("yellowbird-upflap.png")),
|
||||
paradiso::BitmapIO::get().load("yellowbird-upflap.png"),
|
||||
.pivot = paradiso::Vector2<float>::make(0.0f, 0.0f),
|
||||
.scale = paradiso::Vector2<float>::make(scalew, scaleh)}};
|
||||
}
|
||||
|
@ -266,11 +268,16 @@ auto main() -> int {
|
|||
// nothing beats a classic look
|
||||
ctx.set_clearcolor(paradiso::RGBA::from_rgb(0x00, 0x00, 0x00));
|
||||
|
||||
|
||||
// Asset loader bekommt den Pfad
|
||||
|
||||
paradiso::BitmapIO::get().set_path("assets");
|
||||
|
||||
// Load
|
||||
auto background = Background{};
|
||||
auto grass = Grass{};
|
||||
|
||||
auto flappyBird = FlappyBird{};
|
||||
auto quickwingsapp = QuickWings{};
|
||||
|
||||
// timer
|
||||
|
||||
|
@ -285,13 +292,13 @@ auto main() -> int {
|
|||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Keyboard and state change
|
||||
flappyBird.on_keyboard(w.keyboard_input());
|
||||
flappyBird.update();
|
||||
quickwingsapp.on_keyboard(w.keyboard_input());
|
||||
quickwingsapp.update();
|
||||
|
||||
// Draw
|
||||
background.draw(shader);
|
||||
grass.draw(shader);
|
||||
flappyBird.draw(shader);
|
||||
quickwingsapp.draw(shader);
|
||||
|
||||
// wait for frame rate
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|