Hi ^.^
I am new to this forum and i currently working on my first game , Tetris *yay*
I just wanted to share my Tetromino class and get some opinions on how i can improve it
( i tried messing around with C++11 a bit )
This is the header file :
#ifndef TETROMINO_HPP
#define TETROMINO_HPP
#include <array>
#include <SFML\Graphics.hpp>
enum typeTetrominoID { IBrick, JBrick, LBrick, OBrick, SBrick, TBrick, ZBrick };
struct TetrominoCodes
{
using TetrominoCode = std::array < std::array<sf::Uint32, 4>, 2 >;
TetrominoCode I;
TetrominoCode J;
TetrominoCode L;
TetrominoCode O;
TetrominoCode S;
TetrominoCode T;
TetrominoCode Z;
TetrominoCodes()
: I(TetrominoCode{ { { 1, 1, 1, 1 }, { 0, 0, 0, 0 } } }),
J(TetrominoCode{ { { 1, 0, 0, 0 }, { 1, 1, 1, 0 } } }),
L(TetrominoCode{ { { 0, 0, 1, 0 }, { 1, 1, 1, 0 } } }),
O(TetrominoCode{ { { 1, 1, 0, 0 }, { 1, 1, 0, 0 } } }),
S(TetrominoCode{ { { 0, 1, 1, 0 }, { 1, 1, 0, 0 } } }),
T(TetrominoCode{ { { 0, 1, 0, 0 }, { 1, 1, 1, 0 } } }),
Z(TetrominoCode{ { { 1, 1, 0, 0 }, { 0, 1, 1, 0 } } })
{};
};
struct TetrominoColors
{
using TetrominoColor = sf::Color;
TetrominoColor I = TetrominoColor(152, 245, 255);
TetrominoColor J = TetrominoColor(0, 0, 255);
TetrominoColor L = TetrominoColor(255, 165, 79);
TetrominoColor O = TetrominoColor(255, 255, 0);
TetrominoColor S = TetrominoColor(0, 255, 0);
TetrominoColor T = TetrominoColor(160, 32, 240);
TetrominoColor Z = TetrominoColor(255, 0, 0);
};
class Tetromino : public sf::Drawable, public sf::Transformable
{
private:
typeTetrominoID m_typeID;
sf::Texture m_texture;
sf::Vector2u m_brickSize;
sf::VertexArray m_vertices;
void create(TetrominoCodes::TetrominoCode code, TetrominoColors::TetrominoColor color);
virtual void draw(sf::RenderTarget& renderTarget, sf::RenderStates renderStates) const;
public:
Tetromino();
Tetromino(typeTetrominoID typeID);
void updateFixed(void);
void updateVariable(sf::Time dt);
};
#endif // TETROMINO_HPP
And this is the .cpp file :
#include <iostream>
#include <SFML\Graphics.hpp>
#include "Tetromino.hpp"
Tetromino::Tetromino()
{}
Tetromino::Tetromino(typeTetrominoID typeID)
: m_typeID(typeID), m_brickSize(sf::Vector2u(16, 16))
{
TetrominoCodes tetrominoCodes;
TetrominoColors tetrominoColors;
std::cout << "m_typeID: " << m_typeID << std::endl;
switch (m_typeID)
{
case IBrick:
create(tetrominoCodes.I, tetrominoColors.I); break;
case JBrick:
create(tetrominoCodes.J, tetrominoColors.J); break;
case LBrick:
create(tetrominoCodes.L, tetrominoColors.L); break;
case OBrick:
create(tetrominoCodes.O, tetrominoColors.O); break;
case SBrick:
create(tetrominoCodes.S, tetrominoColors.S); break;
case TBrick:
create(tetrominoCodes.T, tetrominoColors.T); break;
case ZBrick:
create(tetrominoCodes.Z, tetrominoColors.Z); break;
}
}
void Tetromino::updateFixed(void)
{
sf::Vector2f pos = this->getPosition();
pos.y += m_brickSize.y;
this->setPosition(pos);
}
void Tetromino::updateVariable(sf::Time dt)
{}
void Tetromino::create(TetrominoCodes::TetrominoCode code, TetrominoColors::TetrominoColor color)
{
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(16);
std::array<std::array<sf::Vertex, 4>, 4> quads;
sf::Uint16 vertexCounter = 0;
for (sf::Uint16 i = 0; i < 2; i++)
{
for (sf::Uint16 j = 0; j < 4; j++)
{
std::cout << "code[" << i << "][" << j << "]: " << code[i][j] << std::endl;
if (code[i][j] == 1)
{
quads[vertexCounter][0].color = color;
quads[vertexCounter][1].color = color;
quads[vertexCounter][2].color = color;
quads[vertexCounter][3].color = color;
quads[vertexCounter][0].position = sf::Vector2f(i * m_brickSize.x, j* m_brickSize.y);
quads[vertexCounter][1].position = sf::Vector2f((i + 1) * m_brickSize.x, j * m_brickSize.y);
quads[vertexCounter][2].position = sf::Vector2f((i + 1) * m_brickSize.x, (j + 1) * m_brickSize.y);
quads[vertexCounter][3].position = sf::Vector2f(i * m_brickSize.x, (j + 1) * m_brickSize.y);
for (auto i : quads[vertexCounter])
{
m_vertices.append(i);
std::cout << "position: " << i.position.x << ", " << i.position.y << std::endl;
}
vertexCounter++;
}
}
}
}
void Tetromino::draw(sf::RenderTarget& renderTarget, sf::RenderStates renderStates) const
{
renderStates.transform *= getTransform();
renderStates.texture = &m_texture;
renderTarget.draw(m_vertices, renderStates);
}
P.s my english isn't always on point ... so yea