If you will just move around, use sf::View for a camera.
If you actually want to transform the whole vertex array without changing the vertices, use sf::Transform.
If you want to draw only a part of the array, then draw it manually and use glDrawArrays(Primitive, StartVertex, NumVertices); (can only draw contiguous vertices)
That's it, there is always a solution. For maps, better to divide it in multiple regions, and draw them whole only if they are within the screen, moving around with a sf::View only. Try
I am making a 2d "shooter"? but with swords&magic era. Projectiles, and real time actions.
What i am afraid off is how will i handle projectiles positioning, and pathing.
Also doing AI... that is gonna be a new.
As you suggested, i will use sf::View for position drawing.
For the map, i will cut it in regions... that will be pretty simple, and fast enough.
If you want to draw only a part of the array, then draw it manually and use glDrawArrays(Primitive, StartVertex, NumVertices); (can only draw contiguous vertices)
You can draw a part of a vertex array with SFML (window.draw(verticesPtr, count, primitiveType)), no need to use OpenGL.
I am wondering, if i pass verticesPtr, a pointer to a vertex that is in middle of vertexArray, count to fill that row until end of screen. primitiveType = Quads. That should work...
UPDATE::
Its possible to pass a vertex that is in middle of vertex array, and draw only limited amount! so cool.
Got it textures and all.
But, it isen't safe. In case of memory corruption or something i will cause huge error that will require handling.
EDIT::
If someone needs / requires a example, here is code:Make a "map.txt" file and copy this intro it.
Don't copy -> "" <- quotation marks
"1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 "
also make sure you have image "img.png" for texture...
const int TILE_SIZE = 32;
int main()
{
//Main window
sf::RenderWindow renWin;
renWin.create(sf::VideoMode(1024, 768, 32), "hy");
//Texture
sf::Texture tex;
tex.loadFromFile("img.png");
int imageWidth = tex.getSize().x / TILE_SIZE;
//Map
sf::VertexArray verArr(sf::PrimitiveType::Quads, 10 * 10 * 4);
std::ifstream ifstream;
ifstream.open("map.txt");
if(ifstream.is_open() == true)
{
//Loading map 10x10 size
for(int y = 0; y < 10; y++)
{
for(int x = 0; x < 10; x++)
{
int readTile;
ifstream >> readTile;
int ID = (x + y * 10) * 4;
verArr[ID].position = sf::Vector2f(x * TILE_SIZE, y * TILE_SIZE);
verArr[ID+1].position = sf::Vector2f(x * TILE_SIZE + TILE_SIZE, y * TILE_SIZE);
verArr[ID+2].position = sf::Vector2f(x * TILE_SIZE + TILE_SIZE, y * TILE_SIZE + TILE_SIZE);
verArr[ID+3].position = sf::Vector2f(x * TILE_SIZE, y * TILE_SIZE + TILE_SIZE);
readTile -=1;//Quick fix for getting correct tile in math
verArr[ID].texCoords = sf::Vector2f((readTile % imageWidth) * TILE_SIZE, (readTile / imageWidth) * TILE_SIZE);
verArr[ID+1].texCoords = sf::Vector2f((readTile % imageWidth) * TILE_SIZE +TILE_SIZE, (readTile / imageWidth) * TILE_SIZE);
verArr[ID+2].texCoords = sf::Vector2f((readTile % imageWidth) * TILE_SIZE + TILE_SIZE, (readTile / imageWidth) * TILE_SIZE + TILE_SIZE);
verArr[ID+3].texCoords = sf::Vector2f((readTile % imageWidth) * TILE_SIZE, (readTile / imageWidth) * TILE_SIZE + TILE_SIZE);
}
}
}
//Vertex to be drawn
//sf::Vertex *ver = &verArr[12];
//Render state that hold texture
sf::RenderStates state;
state.texture = &tex;
while(renWin.isOpen())
{
//Event handling
sf::Event event;
while(renWin.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed: renWin.close(); break;
default: break;
}
}
//Clear
renWin.clear();
//Drawing vertex/vertices
sf::Vertex *ver;
for(int i = 0; i < 5; i++)
{
ver = &verArr[i * 10 * 4 + 12];
renWin.draw(ver, 12, sf::PrimitiveType::Quads, state);
}
//feed buffer
renWin.display();
}
return 0;
}