Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

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.


Topics - robkruger

Pages: [1]
1
Graphics / VertexArray with different textures per vertex
« on: November 27, 2019, 11:21:19 am »
I'm trying to make a terraria clone, and draw the tiles with a VertexArray. The problem is that I don't see a way to set a custom texture for every vertex. Is the only solution I can do to create a container for every tile type and draw a VertexArray for every container?

sf::VertexArray va(sf::Quads, 68 * 18);
    int y = 0;
    int x = 0;
    sf::Texture sprite_sheet;
    sprite_sheet = sfml_resources::get().get_spritesheet();
    unsigned i = 0;
    for(tile& t : m_game.get_tiles()){
      y = (17 * (t.get_state() / 16));
      x = (18 * t.get_state()) % (16 * 18);
      sf::Vertex* quad = &va[i];
      const int tx = static_cast<int>(t.get_x());
      const int ty = static_cast<int>(t.get_y());
      quad[0].position = sf::Vector2f(tx, ty);
      quad[1].position = sf::Vector2f(tx + 32, ty);
      quad[2].position = sf::Vector2f(tx + 32, ty + 32);
      quad[3].position = sf::Vector2f(tx, ty + 32);
      quad[0].texCoords = sf::Vector2f(x, y);
      quad[1].texCoords = sf::Vector2f(x + 16, y);
      quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
      quad[3].texCoords = sf::Vector2f(x, y + 16);
      i += 4;
    }
    m_window.draw(va, &sprite_sheet);

2
General / Graphic problem
« on: September 26, 2019, 07:32:34 pm »
I recently switched to using a vertex array for displaying the tiles, but sometimes I get lines between them, as you can see in the first attachment. And when I move it goes back to normal, as you can see in the second attachment. Does anyone know why this happens?

Code:
sf::VertexArray va(sf::Quads, m_game.get_loaded_chunks().size() * 2048);
int y = 0;
int x = 0;
sf::Texture sprite_sheet;
sprite_sheet = sfml_resources::get().get_spritesheet();
int i = 0;
for(chunk c : m_game.get_loaded_chunks()){
  for(tile& t : c.get_tiles()){
    y = (t.get_type() / 57) * 16;
    x = (17 * t.get_type()) % (57 * 17);
    sf::Vertex* quad = &va[i];
    quad[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
    quad[1].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
    quad[2].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
    quad[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
    quad[0].texCoords = sf::Vector2f(x, y);
    quad[1].texCoords = sf::Vector2f(x + 16, y);
    quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
    quad[3].texCoords = sf::Vector2f(x, y + 16);
    if(t.get_attribute() != 0){
      sf::Vertex* att = &va[i + 4];
      att[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
      att[1].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
      att[2].position = sf::Vector2f(t.get_x() + 32 - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
      att[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 32 - m_game.get_camera().y);
      if(t.get_attribute() == 1){
        att[0].texCoords = sf::Vector2f(221, 153);
        att[1].texCoords = sf::Vector2f(221 + 16, 153);
        att[2].texCoords = sf::Vector2f(221 + 16, 153 + 16);
        att[3].texCoords = sf::Vector2f(221, 153 + 16);
      }
      else if(t.get_attribute() == 2){
        att[0].texCoords = sf::Vector2f(459, 187);
        att[1].texCoords = sf::Vector2f(459 + 16, 187);
        att[2].texCoords = sf::Vector2f(459 + 16, 187 + 16);
        att[3].texCoords = sf::Vector2f(459, 187 + 16);
      }
      else if(t.get_attribute() == 3){
        att[0].texCoords = sf::Vector2f(443, 170);
        att[1].texCoords = sf::Vector2f(443 + 16, 170);
        att[2].texCoords = sf::Vector2f(443 + 16, 170 + 16);
        att[3].texCoords = sf::Vector2f(443, 170 + 16);
      }
    }
    i += 8;
  }
}
m_window.draw(va, &sprite_sheet);

3
General / Access objects in a particular order
« on: September 23, 2019, 08:03:54 pm »
To load the chunks which have to be loaded related to where the camera is in my game, I have written this function, but I can't get it to load the correct chunks. This is my code:

std::vector<chunk> game::load_chunks(){
  std::vector<chunk> loaded_chunks;
  int x = (m_camera.x + 960) / 512;
  int y = (m_camera.y + 540) / 512;
  int width = static_cast<int>(m_width / 512 + 0.99);
  int height = static_cast<int>(m_height / 512 + 0.99);
  int amount = width * height;
  int i;
  std::cout << m_camera.x << " " << m_camera.y << std::endl;
  std::cout << "x " << x << " y " << y << std::endl;
  std::cout << width << " " << height << std::endl;
  std::cout << -(amount / 2) << " " << (amount / 2) << std::endl;
  for(i = -(amount / 2); i < (amount / 2); i++){
    std::cout << x + (i % width) << " " << y + (i / height) << std::endl;
    auto find = m_chunks.find(std::make_pair(x + (i % width), y + (i / height)));
    if(find != m_chunks.end()) loaded_chunks.push_back(find->second);
  }
  m_last_load.x = m_camera.x;
  m_last_load.y = m_camera.y;
  return loaded_chunks;
}

Can anybody help?

4
Graphics / sf::VertexArray issue
« on: September 17, 2019, 03:32:59 pm »
I have this to draw a tile map:

sf::VertexArray va(sf::Quads, 10240);
int y = 0;
int x = 0;
sf::Texture sprite_sheet = sfml_resources::get().get_spritesheet();
int i = 0;
for(chunk c : m_game.get_loaded_chunks()){
   for(tile t : c.get_tiles()){
     y = (16 * (t.get_type() / 57)) / 16;
     x = (17 * t.get_type()) % (57 * 17);
     sf::Vertex* quad = &va[i];
     quad[0].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
     quad[1].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() - m_game.get_camera().y);
     quad[2].position = sf::Vector2f(t.get_x() + 16 - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
     quad[3].position = sf::Vector2f(t.get_x() - m_game.get_camera().x, t.get_y() + 16 - m_game.get_camera().y);
     quad[0].texCoords = sf::Vector2f(x, y);
     quad[1].texCoords = sf::Vector2f(x + 16, y);
     quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
     quad[3].texCoords = sf::Vector2f(x, y + 16);
     i++;
   }
}
m_window.draw(va, &sprite_sheet);

But when I run it the position is weird, see screenshot. Why is this happening?

5
Window / Single key key repetition
« on: September 16, 2019, 08:11:41 pm »
I have this function to process inputs:

void sfml_game::process_input(){
  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) m_game.get_camera().move(sf::Vector2f(-1, 0));
  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) m_game.get_camera().move(sf::Vector2f(1, 0));
  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) m_game.get_camera().move(sf::Vector2f(0, -1));
  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) m_game.get_camera().move(sf::Vector2f(0, 1));
  if(sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) m_debug_items = !m_debug_items;
}

Now I want to disable key repetition for F3 only, because I use it to toggle something. In it current state it just loops trough until I release the button. Is it possible to disable key repetition for one button only?

Pages: [1]