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
7 changes: 6 additions & 1 deletion assets/vars.conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ UI_BUTTON_3D_OFFSET: float = 5.0
UI_TOOLTIP_PADDING: float = 20.0
UI_TOOLTIP_SCALE: float = 3.0
UI_TOOLTIP_FOREGROUND: color = FFFFFFFF
UI_TOOLTIP_BACKGROUND: color = 000000FF
UI_TOOLTIP_BACKGROUND: color = 000000FF

CONSOLE_BACKGROUND: color = 000000EE
CONSOLE_FOREGROUND: color = FFFFFFEE
CONSOLE_ROWS: int = 20
CONSOLE_SCALE: float = 4.0
103 changes: 103 additions & 0 deletions src/fruits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef FRUITS_H_
#define FRUITS_H_

// What? This is just a list of fruits. What did you expect?

const char *fruits[] = {
"Apple",
"Apricot",
"Avocado",
"Banana",
"Bilberry",
"Blackberry",
"Blackcurrant",
"Blueberry",
"Boysenberry",
"Currant",
"Cherry",
"Cherimoya",
"Chico fruit",
"Cloudberry",
"Coconut",
"Cranberry",
"Cucumber",
"Custard apple",
"Damson",
"Date",
"Dragonfruit",
"Durian",
"Elderberry",
"Feijoa",
"Fig",
"Goji berry",
"Gooseberry",
"Grape",
"Raisin",
"Grapefruit",
"Guava",
"Honeyberry",
"Huckleberry",
"Jabuticaba",
"Jackfruit",
"Jambul",
"Jujube",
"Juniper berry",
"Kiwano",
"Kiwifruit",
"Kumquat",
"Lemon",
"Lime",
"Loquat",
"Longan",
"Lychee",
"Mango",
"Mangosteen",
"Marionberry",
"Melon",
"Cantaloupe",
"Honeydew",
"Watermelon",
"Miracle fruit",
"Mulberry",
"Nectarine",
"Nance",
"Olive",
"Orange",
"Blood orange",
"Clementine",
"Mandarine",
"Tangerine",
"Papaya",
"Passionfruit",
"Peach",
"Pear",
"Persimmon",
"Physalis",
"Plantain",
"Plum",
"Prune",
"Pineapple",
"Plumcot",
"Pomegranate",
"Pomelo",
"Purple mangosteen",
"Quince",
"Raspberry",
"Salmonberry",
"Rambutan",
"Redcurrant",
"Salal berry",
"Salak",
"Satsuma",
"Soursop",
"Star fruit",
"Solanum quitoense",
"Strawberry",
"Tamarillo",
"Tamarind",
"Ugli fruit",
"Yuzu"
};
const size_t fruits_count = sizeof(fruits) / sizeof(fruits[0]);

#endif // FRUITS_H_
2 changes: 2 additions & 0 deletions src/something.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Index {
#else
#include "./config_loader.cpp"
#include "./config_parser.cpp"
#include "./fruits.h"
#endif // SOMETHING_RELEASE

#include "./something_atlas.cpp"
Expand All @@ -54,6 +55,7 @@ struct Index {
#include "./something_shader.cpp"
#include "./something_font.cpp"
#include "./something_ui.cpp"
#include "./something_console.cpp"

#define AIDS_IMPLEMENTATION
#include "./aids.hpp"
2 changes: 1 addition & 1 deletion src/something_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct HSLA {
h(h), s(s), l(l), a(a)
{}

// TODO: represent colors as 3-4 dimensional vectors
// TODO(#115): represent colors as 3-4 dimensional vectors
// To enable the mathematical vector operations
RGBA to_rgba() const
{
Expand Down
44 changes: 44 additions & 0 deletions src/something_console.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "something_console.hpp"

void Row::copy_from_sv(String_View sv)
{
count = min(sv.count, CONSOLE_BUFFER_COLS);
memcpy(chars, sv.data, count);
}

void Row::append_from_sv(String_View sv)
{
if (count + sv.count > CONSOLE_BUFFER_COLS) {
sv.count = CONSOLE_BUFFER_COLS - count;
}

memcpy(chars + count, sv.data, sv.count);
count += sv.count;
}

String_View Row::as_sv() const
{
return String_View {count, chars};
}

void Row::clear()
{
count = 0;
}

void Row_Ring::push_line(String_View line)
{
begin = static_cast<size_t>(
mod(static_cast<int>(begin) - 1,
static_cast<int>(CONSOLE_BUFFER_ROWS)));
rows[begin].copy_from_sv(line);

if (count < CONSOLE_BUFFER_ROWS) {
count += 1;
}
}

String_View Row_Ring::get(size_t index)
{
return rows[(begin + index) % CONSOLE_BUFFER_ROWS].as_sv();
}
26 changes: 26 additions & 0 deletions src/something_console.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef SOMETHING_CONSOLE_HPP_
#define SOMETHING_CONSOLE_HPP_

const size_t CONSOLE_BUFFER_COLS = 256;
const size_t CONSOLE_BUFFER_ROWS = 1024;

struct Row {
size_t count;
char chars[CONSOLE_BUFFER_COLS];

void copy_from_sv(String_View sv);
void append_from_sv(String_View sv);
String_View as_sv() const;
void clear();
};

struct Row_Ring {
Row rows[CONSOLE_BUFFER_ROWS];
size_t begin;
size_t count;

void push_line(String_View line);
String_View get(size_t index);
};

#endif // SOMETHING_CONSOLE_HPP_
85 changes: 75 additions & 10 deletions src/something_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ void Game::handle_event(const SDL_Event *event)
println(stdout, aabb_bodies[0].hitbox);
}
break;

#ifndef SOMETHING_RELEASE
case SDLK_x: {
// TODO(#116): console prompt does not handle the user input properly
console_prompt.append_from_sv(cstr_as_string_view(fruits[rand() % fruits_count]));
console_prompt.append_from_sv(" "_sv);
} break;

case SDLK_c: {
console_enabled = !console_enabled;
}
break;

case SDLK_RETURN: {
console_log.push_line(console_prompt.as_sv());
console_prompt.clear();
}
break;
#endif // SOMETHING_RELEASE
}
}
break;
Expand Down Expand Up @@ -173,7 +192,7 @@ void Game::handle_event(const SDL_Event *event)
ui.mouse_button = true;

#ifndef SOMETHING_RELEASE
if (!editor) {
if (!editor_enabled) {
player.shoot(this);
}
#else
Expand All @@ -191,7 +210,7 @@ void Game::handle_event(const SDL_Event *event)
case SDL_KEYDOWN: {
switch (event->key.keysym.sym) {
case SDLK_q: {
editor = !editor;
editor_enabled = !editor_enabled;
}
break;

Expand Down Expand Up @@ -286,7 +305,7 @@ void Game::update(Seconds dt)

#ifndef SOMETHING_RELEASE
{
if (editor) {
if (editor_enabled) {
// Tools Panel
{
const auto screen_size = V2(SCREEN_WIDTH, SCREEN_HEIGHT).cast_to<float>();
Expand Down Expand Up @@ -369,19 +388,65 @@ void Game::update(Seconds dt)
ui.end();
}

// Cursor
{
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);
}

if (console_enabled) {
const auto left_top_corner = V2(SCREEN_WIDTH, SCREEN_HEIGHT).cast_to<float>() * V2(-0.5f, 0.5f);
const auto console_row_height = font.text_size(1.0f, CONSOLE_SCALE).y;
const auto console_height = console_row_height * (CONSOLE_ROWS + 1);
const auto console_pos = left_top_corner - V2(0.0f, console_height);
const auto console_rect = AABB(console_pos, V2(static_cast<float>(SCREEN_WIDTH), console_height));

// Background
{
renderer.fill_aabb(
AABB(mouse_screen - cursor_size * V2(0.0f, 1.0f), cursor_size),
MOUSE_CURSOR_COLOR,
cursor_texture_uv,
console_rect,
CONSOLE_BACKGROUND,
atlas.get_uv({0}),
SCREEN_PROGRAM_ASSET);
}

// Foreground
{
// Log
{
for (int i = 0; i < min(CONSOLE_ROWS, static_cast<int>(console_log.count)); ++i) {
const auto row_pos = console_pos + V2(0.0f, (i + 1) * console_row_height);
font.render_text(
&renderer,
console_log.get(i),
row_pos,
CONSOLE_SCALE,
CONSOLE_FOREGROUND);
}
}

// Prompt
{
const auto prompt_pos = console_pos;
font.render_text(
&renderer,
console_prompt.as_sv(),
prompt_pos,
CONSOLE_SCALE,
CONSOLE_FOREGROUND);
}
}
}

// Cursor
if (editor_enabled || console_enabled) {
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
Expand Down
8 changes: 7 additions & 1 deletion src/something_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "./something_item.hpp"
#include "./something_font.hpp"
#include "./something_ui.hpp"
#include "./something_console.hpp"

struct Game {
static const size_t AABB_BODIES_CAPACITY = 1024;
Expand All @@ -34,9 +35,14 @@ struct Game {
};

// Indicates whether the level editor mode is on
bool editor;
bool editor_enabled;
Editor_Tool editor_tool;
float editor_tool_hue(Editor_Tool tool) const;

// Indicates whether the console is enabled
bool console_enabled;
Row_Ring console_log;
Row console_prompt;
#endif

const Uint8 *keyboard;
Expand Down
2 changes: 1 addition & 1 deletion src/something_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Batch {
};

struct Renderer {
constexpr static size_t CAPACITY = 1024;
constexpr static size_t CAPACITY = 1024 * 16;

Triangle_VAO triangle_vao;

Expand Down
2 changes: 1 addition & 1 deletion src/something_triangle_vao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "./something_camera.hpp"

struct Triangle_VAO {
static const size_t CAPACITY = 1024;
static const size_t CAPACITY = 1024 * 16;

GLuint id;

Expand Down