1
SFML projects / Re: Nero Game Engine
« on: August 19, 2020, 03:41:21 am »
Man every version just gets better and better, good job on this, this is really cool.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
- Nexus
- binary1248
- Tank
- Grimshaw
- eXpl0it3r
- Hiura
- Sonkun
class Cfps
{
private:
int rate;
float startTime;
float lastTime;
int Fps;
int frames;
float speed;
sf::Clock fpsClock;
public:
Cfps();
Cfps(unsigned int _rate);
int getFps();
float getSpeed();
void update();
};
//==================================================================
/// Cfps
//==================================================================
Cfps::Cfps(unsigned int _rate)
{
rate = _rate;
startTime = 0;
lastTime = 0;
Fps = 0;
frames = 0;
speed = 0;
}
Cfps::Cfps()
{
rate = 60;
startTime = 0;
lastTime = 0;
Fps = 0;
frames = 0;
speed = 0;
}
//----------------------------------------------------------------
int Cfps::getFps() {return Fps;}
//==================================================================
float Cfps::getSpeed() {return speed;}
//==================================================================
void Cfps::update()
{
sf::Time ticks = fpsClock.getElapsedTime();
//one second has passed ( 1000 miliseconds )
if (startTime + 1000 < ticks.asMilliseconds())
{
startTime = ticks.asMilliseconds();
Fps = frames;
frames = 0;
}
speed = ( (ticks.asMilliseconds() - lastTime) / 1000 * rate );
lastTime = ticks.asMilliseconds();
frames++;
}
Cfps appFps(60);
appFps.update();
class MyEntity
{
public:
MyEntity() : sprite(m_texture)
{
m_texture.loadFromFile("lucas.png");
sprite.setTextureRect(sf::IntRect(0,0,40,40));
sprite.setPosition(0, 0);
}
virtual void draw(sf::RenderWindow& target) const
{
target.draw(sprite);
}
sf::Sprite sprite;
sf::Texture m_texture;
};
class TileMap : public sf::Drawable, public sf::Transformable
{
public:
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
// load the tileset texture
if (!m_tileset.loadFromFile(tileset))
return false;
// resize the vertex array to fit the level size
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i)
for (unsigned int j = 0; j < height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * width];
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * height * 4);
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
return true;
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
int main()
{
MyEntity entity;
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "Tilemap");
// define the level with an array of tile indices
const int level[] =
{
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
};
// create the tilemap from the level definition
TileMap map;
if (!map.load("tileset.png", sf::Vector2u(32, 32), level, 16, )
return -1;
window.setFramerateLimit(60);
float x = 0, y = 0;
// run the main loop
while (window.isOpen())
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
x+=1; //y+=1;
entity.sprite.setPosition(x,y);
}
// draw the map
window.clear();
window.draw(map);
entity.draw(window);
window.display();
}
return 0;
}
const int level[] =
{
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
};
// create the tilemap from the level definition
TileMap map;
if (!map.load("tileset.png", sf::Vector2u(32, 32), level, 16, )
void Render(PencilTool& renderer)
{
Window.clear(sf::Color(0, 135, 206, 250));
for( size_t i = 0 ; i < objectList.size(); i++)
{
objectList->Render(renderer);
}
Window.display();
}
void Draw(sf::RenderWindow& window, sf::Sprite& object, const float& x, const float& y, const int16_t& x2, const int16_t& y2, const int16_t& w, const int16_t& h)
{
object.setPosition(x,y);
object.setTextureRect(sf::IntRect(x2,y2,w,h));
window.draw(object);
}
for(int y = 0; y < mapHeight; y++)
for(int x = 0; y < mapWidth; x++)
{
v.push_back(tile);
// same as v[y]
- ; you get the point...
}
vector<char> buffer;
Packet & Packet::operator << (const unsigned int & data)
{
unsigned char const * p = reinterpret_cast<unsigned char const*>(&data);
std::copy(p, p + sizeof(data), std::back_inserter(buffer));
return *this;
}
vector<char> buffer;
Packet& Packet::operator >> (unsigned int & data)
{
std::copy(&buffer[pos], &buffer[pos] + sizeof(data), reinterpret_cast<char*>(&data));
pos += sizeof(data);
return *this;
}
*(uInt32*)(&buffer[position]) = data;
data = *(uInt32*)(&buffer[position]);
std::copy(&data, &data+ sizeof(data), std::back_inserter(buffer));
std::copy(buffer[0] + pos, &buffer[0] + pos + sizeof(data), &data);
pos += sizeof(dataSize);
memcpy(&buffer[0], &data, sizeof(data));
AND:
memcpy(&data, &buffer[0], sizeof(data));
Ccamera mapCam(640, 480);
mapEdit oMap("My map", 40, 40, 32, "tileSet.png", mapCam);
Or like this :Ccamera mapCam(640, 480);
mapEdit oMap(mapCam);
oMap.generate("My map", 40, 40, 32, "tileSet.png", mapCam);
Ccamera mapCam(640, 480);
CmapEdit oMap(mapCam);
oMap.loadConfig("map00/map00.nyb");
1. Scrolling : Left/Right, Up/Down ( scrolls through the map ).
2. Placing tiles: mouse left click.
3. Deleting tiles: right mouse click.
4. changing tiles: A/S - A to move forward on the tile sheet and S to move backwards.
5. Save map: just press the X button on your keyboard.