Mark zones in dyna example.

This commit is contained in:
Bartosz Taudul
2026-06-21 23:05:20 +02:00
parent 79e0efac96
commit 9cc8f3752e
9 changed files with 58 additions and 0 deletions

View File

@@ -6,6 +6,8 @@
#include "timer.hpp"
#include "world.hpp"
#include <tracy/Tracy.hpp>
namespace dyna
{
@@ -18,6 +20,7 @@ Bomb::Bomb( int x_, int y_ )
void Bomb::draw()
{
ZoneScoped;
if( stage == Stage::exploding )
return;
@@ -38,6 +41,7 @@ void Bomb::draw()
void Bomb::tick( World& world )
{
ZoneScoped;
delta += Timer::delta;
while( delta > 10 )
@@ -74,6 +78,7 @@ void Bomb::tick( World& world )
void Bomb::explode( World& world )
{
ZoneScoped;
stage = Stage::exploding;
left = 200;
@@ -126,6 +131,7 @@ void Bomb::explode( World& world )
void Bomb::die( World& world )
{
ZoneScoped;
dead = true;
Map& map = world.map();

View File

@@ -4,6 +4,8 @@
#include "texture.hpp"
#include "timer.hpp"
#include <tracy/Tracy.hpp>
namespace dyna
{
@@ -17,6 +19,7 @@ Vortex::Vortex( int gx, int gy )
void Vortex::draw()
{
ZoneScoped;
int frame = static_cast<int>( ( Timer::get_timestamp() - action_start ) / 40 );
switch( action )
@@ -36,6 +39,7 @@ void Vortex::draw()
void Vortex::tick( World& )
{
ZoneScoped;
delta += Timer::delta;
while( delta > 10 )

View File

@@ -2,11 +2,16 @@
#include <SDL3/SDL.h>
#include <tracy/Tracy.hpp>
namespace dyna
{
std::string data_path( const std::string& rel )
{
ZoneScoped;
ZoneText( rel.c_str(), rel.size() );
// SDL_GetBasePath returns the executable's directory (with a trailing
// separator) and is owned by SDL, so cache it for the program's lifetime.
static const std::string base = []

View File

@@ -8,6 +8,7 @@
#include "world.hpp"
#include <SDL3/SDL.h>
#include <tracy/Tracy.hpp>
#include <string>

View File

@@ -73,6 +73,7 @@ void main() {
GLuint compile_shader( GLenum type, const char* src )
{
ZoneScoped;
GLuint s = glCreateShader( type );
glShaderSource( s, 1, &src, nullptr );
glCompileShader( s );
@@ -91,6 +92,7 @@ GLuint compile_shader( GLenum type, const char* src )
bool init_shaders()
{
ZoneScoped;
GLuint vs = compile_shader( GL_VERTEX_SHADER, VERT_SRC );
if( !vs ) return false;
GLuint fs = compile_shader( GL_FRAGMENT_SHADER, FRAG_SRC );
@@ -138,6 +140,7 @@ bool init_shaders()
void init_quad_vao()
{
ZoneScoped;
glGenVertexArrays( 1, &g_vao );
glGenBuffers( 1, &g_vbo );
@@ -162,6 +165,7 @@ void init_quad_vao()
// order. Consecutive quads that share a texture collapse into one draw call.
void flush_batch()
{
ZoneScoped;
if( g_verts.empty() )
return;
@@ -196,6 +200,7 @@ namespace Render
bool init()
{
ZoneScoped;
if( !init_shaders() ) return false;
init_quad_vao();
return true;
@@ -203,6 +208,7 @@ bool init()
void shutdown()
{
ZoneScoped;
if( g_vbo ) glDeleteBuffers( 1, &g_vbo );
if( g_vao ) glDeleteVertexArrays( 1, &g_vao );
if( g_program ) glDeleteProgram( g_program );
@@ -217,6 +223,7 @@ void use_texture( GLuint tex, int layer )
GLuint make_texture( int w, int h, int layers, const void* rgba )
{
ZoneScoped;
GLuint tex = 0;
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D_ARRAY, tex );
@@ -241,6 +248,7 @@ void clear()
void swap()
{
ZoneScoped;
flush_batch();
SDL_GL_SwapWindow( g_window );
FrameMark;
@@ -253,6 +261,7 @@ void alpha( float a )
void draw_quad( const Vertex corners[4] )
{
ZoneScoped;
// Two triangles, vertices appended in submission order so painter ordering
// (and the transient per-monster alpha) is preserved by the batch.
const int idx[6] = { 0, 1, 2, 0, 2, 3 };
@@ -271,6 +280,7 @@ void draw_quad( const Vertex corners[4] )
void draw_sprite( int x, int y )
{
ZoneScoped;
float fx = static_cast<float>( x );
float fy = static_cast<float>( y );
float top = static_cast<float>( h ) - fy;
@@ -291,6 +301,7 @@ void draw_square( int x, int y )
void show_help()
{
ZoneScoped;
Textures::menu.bind();
const float fw = static_cast<float>( w );
@@ -329,6 +340,7 @@ void show_help()
void show_menu()
{
ZoneScoped;
Textures::menu.bind();
Vertex logo[4] = {
@@ -355,6 +367,7 @@ namespace Init
bool all()
{
ZoneScoped;
if( !SDL_Init( SDL_INIT_VIDEO ) )
{
std::fprintf( stderr, "SDL_Init failed: %s\n", SDL_GetError() );
@@ -403,6 +416,7 @@ bool all()
void shutdown()
{
ZoneScoped;
Render::shutdown();
if( g_gl_context ) SDL_GL_DestroyContext( g_gl_context );
if( g_window ) SDL_DestroyWindow( g_window );

View File

@@ -15,6 +15,8 @@
#include <fstream>
#include <sstream>
#include <tracy/Tracy.hpp>
namespace dyna
{
@@ -108,6 +110,9 @@ void Field::draw( int x, int y ) const
Map::Map( const std::string& fn )
{
ZoneScoped;
ZoneText( fn.c_str(), fn.size() );
load( fn );
generate_destructibles();
populate_map();
@@ -117,6 +122,7 @@ Map::~Map() = default;
void Map::load( const std::string& fn )
{
ZoneScoped;
std::ifstream f( fn );
if( !f )
{
@@ -174,6 +180,7 @@ bool Map::monster_ok( int rx, int ry, int pxx, int pyy, int r ) const
void Map::generate_destructibles()
{
ZoneScoped;
int i = destructibles;
while( i != 0 )
{
@@ -189,6 +196,7 @@ void Map::generate_destructibles()
void Map::populate_map()
{
ZoneScoped;
for( int type = 1; type <= 3; type++ )
{
int count = ( type == 1 ) ? m1 : ( type == 2 ) ? m2
@@ -208,6 +216,7 @@ void Map::populate_map()
void Map::draw()
{
ZoneScoped;
for( int ry = 0; ry < Y; ry++ )
for( int rx = 0; rx < X; rx++ )
at( rx, ry ).draw( rx, ry );
@@ -219,6 +228,7 @@ void Map::draw()
void Map::tick( World& world )
{
ZoneScoped;
// Bombs.
for( auto& b : bombs ) b->tick( world );
bombs.erase( std::remove_if( bombs.begin(), bombs.end(),

View File

@@ -6,6 +6,8 @@
#include "timer.hpp"
#include "world.hpp"
#include <tracy/Tracy.hpp>
namespace dyna
{
@@ -77,6 +79,7 @@ Action Monster::rand_dir( const Map& map )
void Monster::think( const Map& map )
{
ZoneScoped;
if( action == Action::wait || action == Action::appear )
{
set_action( rand_dir( map ) );
@@ -108,6 +111,7 @@ void Monster::think( const Map& map )
void Monster::tick( World& world )
{
ZoneScoped;
Map& map = world.map();
delta += Timer::delta;
@@ -189,6 +193,7 @@ const AnimTexture& Monster::texture_for( Action a ) const
void Monster::draw()
{
ZoneScoped;
// The original returns without drawing for unexpected actions; monsters only
// ever hold the actions handled by texture_for, so always draw.
generic_draw( texture_for( action ) );

View File

@@ -6,6 +6,8 @@
#include "timer.hpp"
#include "world.hpp"
#include <tracy/Tracy.hpp>
namespace dyna
{
@@ -19,6 +21,7 @@ Player::Player( int gx, int gy )
void Player::tick( World& world )
{
ZoneScoped;
Map& map = world.map();
delta += Timer::delta;
@@ -79,6 +82,7 @@ void Player::tick( World& world )
void Player::draw()
{
ZoneScoped;
const AnimTexture* tex = nullptr;
switch( action )

View File

@@ -5,6 +5,7 @@
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <tracy/Tracy.hpp>
#include <cstdint>
#include <cstdio>
@@ -41,6 +42,7 @@ using SurfacePtr = std::unique_ptr<SDL_Surface, SurfaceDeleter>;
// failure; the result owns its pixels.
SurfacePtr to_rgba( SDL_Surface* src )
{
ZoneScoped;
if( !src ) return nullptr;
return SurfacePtr{ SDL_ConvertSurface( src, SDL_PIXELFORMAT_RGBA32 ) };
}
@@ -49,6 +51,9 @@ SurfacePtr to_rgba( SDL_Surface* src )
bool Texture::load( const char* fn )
{
ZoneScoped;
ZoneText( fn, strlen( fn ) );
SurfacePtr image{ IMG_Load( fn ) };
if( !image )
{
@@ -85,6 +90,8 @@ void Texture::bind() const
void AnimTexture::load( SDL_Surface* sheet, int tilex, int tiley, int n )
{
ZoneScoped;
SurfacePtr rgba = to_rgba( sheet );
if( !rgba )
{
@@ -140,6 +147,8 @@ AnimTexture vortex_appear, vortex;
void preload()
{
ZoneScoped;
menu.load( data_path( "data/gfx/menu.png" ).c_str() );
sand.load( data_path( "data/gfx/sand.png" ).c_str() );
wall.load( data_path( "data/gfx/wall.png" ).c_str() );