Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add render queue #10

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions assets/intro-fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 300 es

precision highp float;

in vec2 fr_texcoord;

uniform sampler2D alphas;

out vec4 fragColor;

void main()
{
vec4 text_color = vec4(1.0, 0.9, 0.3, 1.0);
vec4 mask = vec4(1.0, 1.0, 1.0, texture(alphas, fr_texcoord).r);
fragColor = text_color * mask;
}
15 changes: 15 additions & 0 deletions assets/intro-vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 300 es

in vec2 in_position;
in vec2 in_texcoord;

uniform mat4 mvp;

out vec2 fr_texcoord;

void main()
{
gl_Position = mvp * vec4(in_position.xy, 0.0, 1.0);
fr_texcoord = in_texcoord;
}

46 changes: 28 additions & 18 deletions nodice/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,35 @@ endif
noinst_LTLIBRARIES = libnodice.la

libnodice_la_SOURCES = \
app.h app.cpp \
board.h board.cpp \
colour.h colour.cpp \
config.h config.cpp \
d4.h d4.cpp \
d6.h d6.cpp \
d8.h d8.cpp \
d12.h d12.cpp \
d20.h d20.cpp \
font.h font.cpp \
fontcache.h fontcache.cpp \
gamestate.h gamestate.cpp \
introstate.h introstate.cpp \
app.h app.cpp \
board.h board.cpp \
colour.h colour.cpp \
config.h config.cpp \
d4.h d4.cpp \
d6.h d6.cpp \
d8.h d8.cpp \
d12.h d12.cpp \
d20.h d20.cpp \
font.h font.cpp \
fontcache.h fontcache.cpp \
gamestate.h gamestate.cpp \
introstate.h introstate.cpp \
mesh.h mesh.cpp \
maths.h \
object.h object.cpp \
opengl.h opengl.cpp \
playstate.h playstate.cpp \
shape.h shape.cpp \
video.h video.cpp \
object.h object.cpp \
opengl.h opengl.cpp \
openglmesh.h openglmesh.cpp \
openglshaderpipeline.h openglshaderpipeline.cpp \
opengltexture.h opengltexture.cpp \
playstate.h playstate.cpp \
renderable.h renderable.cpp \
shadercache.h shadercache.cpp \
shaderpipeline.h shaderpipeline.cpp \
shaderstage.h shaderstage.cpp \
shape.h shape.cpp \
texture.h texture.cpp \
vertexbuffer.h vertexbuffer.cpp \
video.h video.cpp \
videocontext.h \
$(vcontext_SOURCES)

Expand Down
26 changes: 25 additions & 1 deletion nodice/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <iostream>
#include "nodice/config.h"
#include "nodice/introstate.h"
#include "nodice/openglmesh.h"
#include "nodice/opengltexture.h"
#include "nodice/video.h"
#include <SDL.h>

Expand Down Expand Up @@ -68,7 +70,8 @@ App(NoDice::Config* config)
: config_(config)
, sdl_init_()
, video_(config)
, font_cache_(config)
, shader_cache_(this)
, font_cache_(this)
, game_is_running_(false)
{
std::srand(std::time(NULL));
Expand Down Expand Up @@ -125,13 +128,34 @@ config()
}


NoDice::ShaderCache& NoDice::App::
shader_cache()
{
return shader_cache_;
}


NoDice::FontCache& NoDice::App::
font_cache()
{
return font_cache_;
}


NoDice::Mesh::OwningPtr NoDice::App::
create_mesh()
{
return Mesh::OwningPtr(new OpenGLMesh());
}


NoDice::Texture::OwningPtr NoDice::App::
create_texture()
{
return Texture::OwningPtr(new OpenGLTexture());
}


void NoDice::App::
stop_game()
{
Expand Down
13 changes: 13 additions & 0 deletions nodice/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include "nodice/fontcache.h"
#include "nodice/gamestate.h"
#include "nodice/mesh.h"
#include "nodice/shadercache.h"
#include "nodice/texture.h"
#include "nodice/video.h"
#include <stack>
#include <vector>
Expand Down Expand Up @@ -57,9 +60,18 @@ namespace NoDice
Config&
config();

ShaderCache&
shader_cache();

FontCache&
font_cache();

Mesh::OwningPtr
create_mesh();

Texture::OwningPtr
create_texture();

private:
/** @brief A special RAII object for SDL initialization and teardown. */
struct SdlInit
Expand All @@ -84,6 +96,7 @@ namespace NoDice
Config* config_;
SdlInit sdl_init_;
Video video_;
ShaderCache shader_cache_;
FontCache font_cache_;
bool game_is_running_;
StateStack state_stack_;
Expand Down
65 changes: 35 additions & 30 deletions nodice/board.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/**
* @file nodice/board.cpp
* @brief Implemntation of the no-dice board module.
*/
/*
* Copyright 2010, 2011, 2017 Stephen M. Webb <[email protected]>
*
* Copyright 2010, 2011 Stephen M. Webb <[email protected]>
* This file is part of no-dice.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of Version 2 of the GNU General Public License as
* published by the Free Software Foundation.
* No-dice is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* No-dice is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with no-dice. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nodice/board.h"

#include <algorithm>
#include <iostream>
#include "nodice/config.h"
#include "nodice/object.h"
Expand All @@ -39,8 +43,8 @@ Board(NoDice::Config const* config)
{
for (int x = 0; x < config_->board_size(); ++x)
{
objects_.push_back(ObjectPtr(new Object(NoDice::chooseAShape(),
Vector3f(x * 2.0f, y * 2.0f, 0.0f))));
objects_.push_back(ObjectPtr(new Object(NoDice::choose_a_shape(),
vec3{x * 2.0f, y * 2.0f, 0.0f})));
}
}
if (find_wins().size() > 0)
Expand All @@ -51,7 +55,7 @@ Board(NoDice::Config const* config)


NoDice::ObjectPtr& NoDice::Board::
at(const NoDice::Vector2i& p)
at(const NoDice::ivec2& p)
{ return objects_[p.x + p.y * config_->board_size()]; }


Expand All @@ -63,10 +67,11 @@ at(int x, int y) const
void NoDice::Board::
update()
{
for (auto it = objects_.begin(); it != objects_.end(); ++it)
{
(*it)->update();
}
using namespace std;

for_each(begin(objects_), end(objects_), [](auto const& obj) {
obj->update();
});

switch (state_)
{
Expand All @@ -78,7 +83,7 @@ update()
at(swap_obj_[0]).swap(at(swap_obj_[1]));
for (auto it = objects_.begin(); it != objects_.end(); ++it)
{
(*it)->setVelocity(Vector3f(0.0f, 0.0f, 0.0f));
(*it)->setVelocity(vec3{0.0f, 0.0f, 0.0f});
}
state_ = state_idle;
}
Expand Down Expand Up @@ -106,15 +111,15 @@ update()
}
else if (drop > 0)
{
at(x, y)->startFalling(Vector3f(2.0f * x, 2.0f * (y - drop), 0.0f));
falling_queue_.push_back(std::make_pair(Vector2i(x, y),
Vector2i(x, y - drop)));
at(x, y)->startFalling(vec3(2.0f * x, 2.0f * (y - drop), 0.0f));
falling_queue_.push_back(std::make_pair(ivec2(x, y),
ivec2(x, y - drop)));
}
}
for (int y = 0; y < drop; ++y)
{
int new_y = config_->board_size() - y - 1;
create_queue_.push_back(Vector2i(x, new_y));
create_queue_.push_back(ivec2(x, new_y));
}
}
removal_queue_.clear();
Expand All @@ -141,8 +146,8 @@ update()
// Fill in the blanks.
for (auto it = create_queue_.begin(); it != create_queue_.end(); ++it)
{
at(*it) = ObjectPtr(new Object(NoDice::chooseAShape(),
Vector3f((*it).x * 2.0f, (*it).y * 2.0f, 0.0f)));
at(*it) = ObjectPtr(new Object(NoDice::choose_a_shape(),
vec3((*it).x * 2.0f, (*it).y * 2.0f, 0.0f)));
}
create_queue_.clear();

Expand Down Expand Up @@ -173,18 +178,18 @@ draw() const


void NoDice::Board::
start_swap(NoDice::Vector2i pos1, NoDice::Vector2i pos2)
start_swap(NoDice::ivec2 pos1, NoDice::ivec2 pos2)
{
swap_obj_[0] = pos1;
swap_obj_[1] = pos2;
ObjectPtr obj1 = at(pos1.x, pos1.y);
ObjectPtr obj2 = at(pos2.x, pos2.y);
obj1->setVelocity(Vector3f(float(pos2.x-pos1.x) / swap_factor,
float(pos2.y-pos1.y) / swap_factor,
0.0f));
obj2->setVelocity(Vector3f(float(pos1.x-pos2.x) / swap_factor,
float(pos1.y-pos2.y) / swap_factor,
0.0f));
obj1->setVelocity(vec3{float(pos2.x-pos1.x) / swap_factor,
float(pos2.y-pos1.y) / swap_factor,
0.0f});
obj2->setVelocity(vec3{float(pos1.x-pos2.x) / swap_factor,
float(pos1.y-pos2.y) / swap_factor,
0.0f});
swap_step_ = 0.0f;
state_ = state_swapping;
}
Expand Down Expand Up @@ -242,7 +247,7 @@ find_wins()
ObjectBag brace;
for (int i = 0; i < count; ++i)
{
const Vector2i p(x+i, y);
const ivec2 p(x+i, y);
removal_queue_.push_back(p);
ObjectPtr& o = at(p);
brace.push_back(o);
Expand Down Expand Up @@ -281,7 +286,7 @@ find_wins()
ObjectBag brace;
for (int i = 0; i < count; ++i)
{
const Vector2i p(x, y+i);
const ivec2 p{x, y+i};
removal_queue_.push_back(p);
ObjectPtr& o = at(p);
brace.push_back(o);
Expand Down
14 changes: 7 additions & 7 deletions nodice/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace NoDice
draw() const;

void
start_swap(Vector2i objPos1, Vector2i objPos2);
start_swap(ivec2 objPos1, ivec2 objPos2);

void
un_swap();
Expand All @@ -66,13 +66,13 @@ namespace NoDice
find_wins();

private:
ObjectPtr& at(const Vector2i& point);
ObjectPtr& at(const ivec2& point);

private:
typedef std::vector<Vector2i> RemovalQueue;
typedef std::pair<Vector2i, Vector2i> MovePair;
typedef std::vector<MovePair> FallingQueue;
typedef std::vector<Vector2i> CreateQueue;
using RemovalQueue = std::vector<ivec2>;
using MovePair = std::pair<ivec2, ivec2>;
using FallingQueue = std::vector<MovePair>;
using CreateQueue = std::vector<ivec2>;

enum State
{
Expand All @@ -86,7 +86,7 @@ namespace NoDice
ObjectBag objects_;
State state_;
float swap_step_;
Vector2i swap_obj_[2];
ivec2 swap_obj_[2];
RemovalQueue removal_queue_;
FallingQueue falling_queue_;
CreateQueue create_queue_;
Expand Down
Loading