Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CREDITS.org
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
- https://opengameart.org/content/mech-stomp-step-sound
- https://opengameart.org/content/crystals-0
- https://opengameart.org/content/futuristic-gun
- https://opengameart.org/content/pizza-cursor
29 changes: 29 additions & 0 deletions assets/shaders/vert/triangle_screen.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Vertex shader that renders a triangle with a particular `uv`
// coordinates and `color` with applied camera_projection
#version 330 core

precision mediump float;

uniform vec2 resolution;
uniform float time;

layout(location = 0) in vec2 vertex_position;
layout(location = 1) in vec4 vertex_color;
layout(location = 2) in vec2 vertex_uv;

out vec4 color;
out vec2 uv;

vec2 screen_projection(vec2 position)
{
return 2.0 * position / resolution;
}

void main() {
gl_Position = vec4(
screen_projection(vertex_position),
0.0,
1.0);
color = vertex_color;
uv = vertex_uv;
}
3 changes: 2 additions & 1 deletion assets/textures/atlas.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ assets/textures/pajaDank.png
assets/textures/tile-grass.png
assets/textures/tile-dirt.png
assets/textures/tsodinclown.png
assets/textures/64.png
assets/textures/64.png
assets/textures/pizzaslice.png
Binary file added assets/textures/pizzaslice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion assets/vars.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ ITEM_IDLE_FREQUENCY: float = 4.0

ENEMY_SPEED: float = 500.0

GRAVITY: float = 4000.0
GRAVITY: float = 4000.0

MOUSE_CURSOR_SIZE: float = 100.0
MOUSE_CURSOR_COLOR: color = FFFFFFFF
MOUSE_CURSOR_TEXTURE: int = 12

DEBUG_TEXT_SCALE: float = 5.0
DEBUG_TEXT_PADDING: float = 20.0
DEBUG_TEXT_COLOR: color = FF0000FF

TOOLS_PANEL_PADDING: float = 50.0
TOOL_BUTTON_SIZE: float = 100.0
15 changes: 12 additions & 3 deletions src/something_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Atlas Atlas::from_config(const char *file_path, int margin)
const float uv_h = static_cast<float>(h) / static_cast<float>(atlas_height);

result.uvs.push(AABB(V2(uv_x, uv_y), V2(uv_w, uv_h)));
result.sizes.push(V2(w, h));
}

atlas_row += 2 * margin + textures.data[i].height;
Expand All @@ -71,8 +72,16 @@ Atlas Atlas::from_config(const char *file_path, int margin)
return result;
}

AABB<float> Atlas::get_uv(Index<AABB<float>> uv_index) const
AABB<float> Atlas::get_uv(Index<AABB<float>> texture_index) const
{
assert(uv_index.unwrap < uvs.size);
return uvs.data[uv_index.unwrap];
assert(texture_index.unwrap < uvs.size);
return uvs.data[texture_index.unwrap];
}

V2<float> Atlas::get_size(Index<AABB<float>> texture_index, float height) const
{
assert(texture_index.unwrap < uvs.size);
const auto size = sizes.data[texture_index.unwrap].cast_to<float>();
const auto aspect = size.x / size.y;
return V2(height * aspect, height);
}
6 changes: 5 additions & 1 deletion src/something_atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ struct Atlas {
Texture texture;
GL_Texture gl_texture;
Dynamic_Array<AABB<float>> uvs;
Dynamic_Array<V2<int>> sizes;

static Atlas from_config(const char *file_path, int margin);

AABB<float> get_uv(Index<AABB<float>> uv_index) const;
// TODO(#96): introduce a special type Atlas::Index instead of Index<AABB<float>>
// TODO(#97): introduce a special type in vars.conf for Atlas::Index
AABB<float> get_uv(Index<AABB<float>> texture_index) const;
V2<float> get_size(Index<AABB<float>> texture_index, float height) const;
};

#endif // SOMETHING_ATLAS_HPP_
1 change: 0 additions & 1 deletion src/something_enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void Enemy::update(Game *game, Seconds)
if (game->projectiles.states[i] == Projectiles::State::Aliv && body.hitbox.contains(game->projectiles.positions[i])) {
game->projectiles.states[i] = Projectiles::State::Ded;
kill(game);
game->spawn_enemy(V2(1947.0f, 1818.5f));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/something_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ void Font::render_text(Renderer *renderer, const char *text, V2<GLfloat> positio
FONT_PROGRAM_ASSET);
}
}

V2<GLfloat> Font::text_size(const char *text, GLfloat scale) const
{
return char_size_pix.cast_to<float>() * V2(scale) * V2(strlen(text), static_cast<size_t>(1)).cast_to<float>();
}
2 changes: 2 additions & 0 deletions src/something_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct Font {

static Font from_file(const char *file_path);
AABB<GLfloat> char_uv(char c) const;

V2<GLfloat> text_size(const char *text, GLfloat scale) const;

void render_text(Renderer *renderer,
const char *text,
Expand Down
120 changes: 120 additions & 0 deletions src/something_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ void Game::handle_event(const SDL_Event *event)
println(stdout, aabb_bodies[0].hitbox);
}
break;

#ifndef SOMETHING_RELEASE
case SDLK_F3: {
editor = !editor;
}
break;

case SDLK_z: {
editor_tool = Editor_Tool::Tiles;
}
break;

case SDLK_x: {
editor_tool = Editor_Tool::Enemies;
}
break;
#endif
}
}
break;
Expand All @@ -135,7 +152,33 @@ void Game::handle_event(const SDL_Event *event)
break;

case SDL_MOUSEBUTTONDOWN: {
#ifndef SOMETHING_RELEASE
if (editor) {
// TODO(#98): the level editor does not allow to "draw" the tiles by dragging the mouse cursor
switch (editor_tool) {
case Editor_Tool::Tiles: {
Tile *tile = tile_grid.get_tile(World_Coord(mouse_world));
if (tile != NULL) {
tile->wall = !tile->wall;
}
}
break;

case Editor_Tool::Enemies: {
spawn_enemy(mouse_world);
}
break;

case Editor_Tool::Count:
default:
aids::unreachable();
}
} else {
player.shoot(this);
}
#else
player.shoot(this);
#endif
}
break;

Expand Down Expand Up @@ -232,6 +275,68 @@ void Game::render(Renderer *renderer) const
items[i].render(this, renderer);
}
particles.render(renderer);

#ifndef SOMETHING_RELEASE
if (editor) {
// Tools Panel
{
const auto screen_size = V2(SCREEN_WIDTH, SCREEN_HEIGHT).cast_to<float>();
const auto tools_panel_pos =
screen_size * V2(-0.5f) + V2(TOOLS_PANEL_PADDING);

// TODO(#100): editor tools panel does not allow to select tools by clicking on their icons
// TODO(#101): editor tools panel buttons don't have any icons on them

for (size_t i = 0; i < static_cast<size_t>(Editor_Tool::Count); ++i) {
const auto tool = static_cast<Editor_Tool>(i);
const auto tool_pos =
tools_panel_pos
+ V2(i, static_cast<size_t>(0)).cast_to<float>()
* V2(TOOL_BUTTON_SIZE + TOOLS_PANEL_PADDING);
renderer->fill_aabb(
AABB(tool_pos, V2(TOOL_BUTTON_SIZE)),
tool == editor_tool ? editor_tool_color(tool) : RGBA(1.0f),
atlas.get_uv({0}),
SCREEN_PROGRAM_ASSET);
}
}

// Cursor
{
const auto mouse_screen =
window_to_viewport(window, mouse_window) -
V2(SCREEN_WIDTH, SCREEN_HEIGHT).cast_to<float>() * V2(0.5f);
const auto cursor_texture_uv = atlas.get_uv({static_cast<size_t>(MOUSE_CURSOR_TEXTURE)}).flip_vertically();
const auto cursor_size = atlas.get_size({static_cast<size_t>(MOUSE_CURSOR_TEXTURE)}, MOUSE_CURSOR_SIZE);

renderer->fill_aabb(
AABB(mouse_screen - cursor_size * V2(0.0f, 1.0f), cursor_size),
MOUSE_CURSOR_COLOR,
cursor_texture_uv,
SCREEN_PROGRAM_ASSET);
}
}

// Debug Label
{
// TODO(#102): vars.conf does not support string type

const char *debug_text = "debug";
const auto debug_size = font.text_size(debug_text, DEBUG_TEXT_SCALE);
const auto screen_size = V2(SCREEN_WIDTH, SCREEN_HEIGHT).cast_to<float>();
const auto debug_position =
screen_size * V2(0.5f, -0.5f) -
debug_size * V2(1.0f, 0.0f) +
V2(DEBUG_TEXT_PADDING) * V2(-1.0f, 1.0f);

font.render_text(
renderer,
debug_text,
debug_position,
DEBUG_TEXT_SCALE,
DEBUG_TEXT_COLOR);
}
#endif
}
renderer->draw(this);
}
Expand Down Expand Up @@ -274,3 +379,18 @@ void Game::spawn_enemy(V2<float> pos)
enemies_size += 1;
}
}

#ifndef SOMETHING_RELEASE
RGBA Game::editor_tool_color(Editor_Tool tool) const
{
switch(tool) {
case Editor_Tool::Tiles:
return RGBA::GREEN();
case Editor_Tool::Enemies:
return RGBA::RED();
case Editor_Tool::Count:
default:
aids::unreachable("Game::editor_tool_color()");
}
}
#endif // SOMETHING_RELEASE
15 changes: 15 additions & 0 deletions src/something_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ struct Game {

bool quit;

// TODO(#99): decouple editor from the game
#ifndef SOMETHING_RELEASE
enum class Editor_Tool: size_t {
Tiles,
Enemies,
Count
};

// Indicates whether the level editor mode is on
bool editor;
Editor_Tool editor_tool;

RGBA editor_tool_color(Editor_Tool tool) const;
#endif

const Uint8 *keyboard;
SDL_Window *window;

Expand Down
4 changes: 4 additions & 0 deletions src/something_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ int main(int argc, char *argv[])
}
defer(SDL_DestroyWindow(window));

if (SDL_ShowCursor(0) < 0) {
panic("SDL ERROR: ", SDL_GetError());
}

{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
Expand Down
10 changes: 7 additions & 3 deletions src/something_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ void Renderer::init()
Shader::make(GL_FRAGMENT_SHADER, "./assets/shaders/frag/texture_color.frag");
shaders[TRIANGLE_CAMERA_VERT_SHADER_ASSET] =
Shader::make(GL_VERTEX_SHADER, "./assets/shaders/vert/triangle_camera.vert");
shaders[TRIANGLE_SCREEN_VERT_SHADER_ASSET] =
Shader::make(GL_VERTEX_SHADER, "./assets/shaders/vert/triangle_screen.vert");
shaders[HSL_FRAG_SHADER_ASSET] =
Shader::make(GL_FRAGMENT_SHADER, "./assets/shaders/frag/hsl.frag");
static_assert(COUNT_SHADER_ASSETS == 4, "The amount of shader assets have changed");
static_assert(COUNT_SHADER_ASSETS == 5, "The amount of shader assets have changed");

programs[REGULAR_PROGRAM_ASSET] =
Program::make({TRIANGLE_CAMERA_VERT_SHADER_ASSET}, {TEXTURE_COLOR_FRAG_SHADER_ASSET});
programs[SCREEN_PROGRAM_ASSET] =
Program::make({TRIANGLE_SCREEN_VERT_SHADER_ASSET}, {TEXTURE_COLOR_FRAG_SHADER_ASSET});
programs[PARTICLE_PROGRAM_ASSET] =
Program::make({TRIANGLE_CAMERA_VERT_SHADER_ASSET}, {GRADIENT_CIRCLE_FRAG_SHADER_ASSET});
programs[PRIDE_PROGRAM_ASSET] =
Program::make({TRIANGLE_CAMERA_VERT_SHADER_ASSET}, {HSL_FRAG_SHADER_ASSET});
programs[FONT_PROGRAM_ASSET] =
Program::make({TRIANGLE_CAMERA_VERT_SHADER_ASSET}, {TEXTURE_COLOR_FRAG_SHADER_ASSET}, 1);
static_assert(COUNT_PROGRAM_ASSETS == 4, "The amount of program assets have changed");
Program::make({TRIANGLE_SCREEN_VERT_SHADER_ASSET}, {TEXTURE_COLOR_FRAG_SHADER_ASSET}, 1);
static_assert(COUNT_PROGRAM_ASSETS == 5, "The amount of program assets have changed");
}

void Renderer::clear()
Expand Down
2 changes: 2 additions & 0 deletions src/something_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ enum Shader_Asset: size_t {
GRADIENT_CIRCLE_FRAG_SHADER_ASSET = 0,
TEXTURE_COLOR_FRAG_SHADER_ASSET,
TRIANGLE_CAMERA_VERT_SHADER_ASSET,
TRIANGLE_SCREEN_VERT_SHADER_ASSET,
HSL_FRAG_SHADER_ASSET,
COUNT_SHADER_ASSETS,
};

enum Program_Asset: size_t {
REGULAR_PROGRAM_ASSET = 0,
SCREEN_PROGRAM_ASSET,
PARTICLE_PROGRAM_ASSET,
PRIDE_PROGRAM_ASSET,
FONT_PROGRAM_ASSET,
Expand Down