adding gameplay buffer

This commit is contained in:
Noel Berry 2021-01-02 13:51:50 -08:00
parent 7d917a98a2
commit 8db43ce18e
6 changed files with 182 additions and 2 deletions

View File

@ -10,9 +10,11 @@ add_subdirectory(libs/blah)
# add our source
add_executable(game
src/main.cpp
src/world.cpp
)
src/game.cpp
src/content.cpp
)
# Reference blah
target_link_libraries(game blah)

40
src/content.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "content.h"
using namespace TL;
namespace
{
FilePath root;
}
SpriteFont Content::font;
FilePath Content::path()
{
if (root.length() <= 0)
{
FilePath up = "";
do
{
root = Path::normalize(FilePath::fmt("%s/%scontent/", App::path(), up.cstr()));
up.append("../");
} while (!Directory::exists(root) && up.length() < 30);
if (!Directory::exists(root))
BLAH_ERROR("Unable to find content directory!");
Log::print("Content Path: %s", root.cstr());
}
return root;
}
void Content::load()
{
font = SpriteFont(path() + "fonts/dogica.ttf", 8, SpriteFont::ASCII);
}
void Content::unload()
{
font.dispose();
}

17
src/content.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <blah.h>
using namespace Blah;
namespace TL
{
class Content
{
public:
static SpriteFont font;
static FilePath path();
static void load();
static void unload();
};
}

55
src/game.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "game.h"
#include "content.h"
using namespace TL;
void Game::startup()
{
// load our content
Content::load();
// framebuffer for the game
buffer = FrameBuffer::create(320, 180);
// set batcher to use Nearest Filter
batch.default_sampler = TextureSampler(TextureFilter::Nearest);
}
void Game::shutdown()
{
}
void Game::update()
{
}
void Game::render()
{
// draw gameplay stuff
{
buffer->clear(Color::red);
batch.str(Content::font, "Hello World", Vec2(32, 32), Color::white);
batch.render(buffer);
batch.clear();
}
// draw buffer to the screen
{
float scale = Calc::min(
App::backbuffer->width() / (float)buffer->width(),
App::backbuffer->height() / (float)buffer->height());
Vec2 screen_center = Vec2(App::backbuffer->width(), App::backbuffer->height()) / 2;
Vec2 buffer_center = Vec2(buffer->width(), buffer->height()) / 2;
App::backbuffer->clear(Color::black);
batch.push_matrix(Mat3x2::create_transform(screen_center, buffer_center, Vec2::one * scale, 0));
batch.tex(buffer->attachment(0), Vec2::zero, Color::white);
batch.pop_matrix();
batch.render(App::backbuffer);
batch.clear();
}
}

21
src/game.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <blah.h>
#include "world.h"
using namespace Blah;
namespace TL
{
class Game
{
public:
World world;
FrameBufferRef buffer;
Batch batch;
void startup();
void shutdown();
void update();
void render();
};
}

45
src/main.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <blah.h>
#include "game.h"
using namespace Blah;
using namespace TL;
namespace
{
Game game;
void startup()
{
game.startup();
}
void shutdown()
{
game.shutdown();
}
void update()
{
game.update();
}
void render()
{
game.render();
}
}
int main()
{
Config config;
config.name = "Tiny Link";
config.width = 1280;
config.height = 720;
config.on_startup = startup;
config.on_shutdown = shutdown;
config.on_update = update;
config.on_render = render;
App::run(&config);
}