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 - mos

Pages: [1]
1
General / Game runs very laggy on my computer (Noticeable CPU spikes)
« on: March 17, 2017, 11:31:25 pm »
When I look at the monitor in Visual Studio, I see the CPU usage spiking from 1%-7% constantly. So I think some of my code is bad or I'm not doing the timing correct. I followed this tutorial:

http://gafferongames.com/game-physics/fix-your-timestep/

This is what it looks like:

https://gfycat.com/SoftLikableDairycow

As you can see, it seems like it is skipping some draw calls. I tried setting the FramerateLimit to 60, 120, and unlimited but get the same results. I tried turning on VSync but get worse performance.

My specs are: Windows 10 64-bit, nVidia 970, i7-4790k

You can download the visual studio project here (Run using x86): https://www.dropbox.com/s/o9uqarh4q3p8uap/RPGWorld.zip?dl=0

Or you can look at the code here: https://gist.github.com/anonymous/2af3c35d568324fb0a6ce8039ef42a64 The relevant code is most likely in Game.cpp where I do the timing in the function update() and Player.cpp where I do the movement.

Another thing to note is that I let someone else try the project and it ran smoothly for them. He was using a Linux machine but same graphics card. Is it Windows related? Any help would be appreciated.

2
General / Multiple tile layers
« on: September 17, 2016, 08:26:50 pm »
Hello, I followed this tutorial: http://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php and was able to draw a simple map. However, I can't figure out how to add a tile on top of another tile.

My tile sheet looks like this:



As you can see, some have a background while the others have a transparent one. If I draw the transparent background tiles next to one that isn't, it looks out of place:



So I wanted to draw another tile underneath it; like grass or sand. Do I have to make 2 arrays? I read my array from a XML file:

<?xml version="1.0" encoding="UTF-8"?>

<Level>
        <!-- Location of tile sheet -->
        <Tileset>Resources/Tiles/medieval.png</Tileset>

        <!-- Number of rows and columns on sheet -->
        <SheetRows>7</SheetRows>
        <SheetColumns>18</SheetColumns>
       
        <!-- Size of the world -->
        <WorldWidth>16</WorldWidth>
        <WorldHeight>10</WorldHeight>
       
        <!-- Offsets -->
        <TopOffset>0</TopOffset>
        <BottomOffset>32</BottomOffset>
        <LeftOffset>0</LeftOffset>
        <RightOffset>32</RightOffset>

        <!-- Tiles from sheet -->
        <Map>
                <Row>75  4   75  1   1   1   0   0   1   0   0   0   0   0   0   0</Row>
                <Row>0   4   75  1   0   0   0   0   0   0   44  0   1   57  0   0</Row>
                <Row>0   4   0   75  0   0   0   1   1   0   4   0   0   0   0   0</Row>
                <Row>0   40  24  72  0   0   0   0   0   0   4   0   0   0   0   0</Row>
                <Row>0   0   0   1   0   0   0   0   0   57  4   0   1   0   0   0</Row>
                <Row>0   0   0   0   0   0   0   1   1   0   4   0   0   0   0   0</Row>
                <Row>0   0   0   0   0   0   0   0   0   0   4   0   0   0   0   0</Row>
                <Row>0   0   0   1   0   0   57  0   0   0   4   0   1   0   0   0</Row>
                <Row>0   0   0   1   0   0   57  0   0   0   4   0   1   0   0   0</Row>
                <Row>0   0   0   0   0   0   0   1   1   0   4   0   0   0   0   0</Row>
        </Map>
</Level>
 

Here's my TileMap.cpp code: https://gist.github.com/anonymous/f9ddbf24f684d0552d7fee31f4063155

What's the best way to achieve multiple layers? Any help would be appreciated!

3
General / FPS Stutter (Windows 10)
« on: September 05, 2016, 04:41:18 am »
Hello, I am having trouble making my game run smooth. It only runs smoothly when I don't limit the framerate. So like this:

window.setFramerateLimit(0);
window.setVerticalSyncEnabled(false);

I change any one of those then you can see some jitter. If I set the framelimit to 60 or turn vync on and display the FPS, you can see it jumping around from 20-65 and sometimes to the thousands.

With the help of jagoly in irc, we were able to implement a fixed timestep and it the stutter less noticeable. However, it is still there on my machine at least. jagoly tested it on his machine and it ran smooth for him no matter if the fps was limited or not.

Why is it not like that on mine? I'm running Windows 10 64bit with a Nvidia GTX 970. Vsync works in other applications just fine for me. Here's my code:

Game.h: https://gist.github.com/anonymous/eccb8b49d41581dcb4062d0a917aac15
Game.cpp: https://gist.github.com/anonymous/92fc02ad0e1aff55deb9682244c1f6c1
Player.h: https://gist.github.com/anonymous/40e2bf3bc4f2d812db34f2e10b8afa12
Player.cpp: https://gist.github.com/anonymous/655f2fd6f7debedfe21974ef89f97af9

Whole project if you want to run it: https://dl.dropboxusercontent.com/u/735021/FPStutter.zip

Any help would be appreciated. Thank you!

4
General / How to tell if no key is pressed?
« on: September 03, 2016, 03:47:01 am »
I looked in the documentation (http://www.sfml-dev.org/documentation/2.4.0/classsf_1_1Keyboard.php) but can't seem to find anything for real time keyboard input that handles if any key is pressed or not. I am trying to make a sprite move and reset its frame in the spritesheet when nothing is pressed.

I know I can do this:

if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (!sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && etc...)
{
    // Reset stuff
}

However, this will take a long time depending on how many actions there are for the sprite. Is there a faster way?

5
General / Spritesheet movement update
« on: September 03, 2016, 12:21:03 am »
Hello, I just started learning SFML and need help with sprite movement. I am able to get a specific frame in the sheet and face it the correct direction depending on the key pressed (left, down, up, right). However, sometimes the sprite faces in another direction when I move my mouse even though I have no mouse events. Another problem I have is that it is not that responsive. If I spam left and right arrow keys, there is sometimes a delay before it faces it another direction (same with any two key combination spams).

Spritesheet: http://i.imgur.com/mvqg4CI.png

Here is my code:

Player.h
#pragma once
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>

class Player
{
        private:
                sf::Texture texture;
                sf::Sprite sprite;
                sf::Vector2i source;
                sf::Vector2f position;

                unsigned int rows, columns;
                unsigned int spriteWidth, spriteHeight;
                unsigned int sheetWidth, sheetHeight;

                // Corresponds to the row
                enum Direction { Up, Left, Down, Right };

        public:
                Player(std::string filename, int rows, int columns, sf::Vector2f position);

                void update(sf::Event event);
                void draw(sf::RenderWindow &window);
};
 

Player.cpp
#include "Player.h"

Player::Player(std::string filename, int rows, int columns, sf::Vector2f position)
{
        // Load the texture
        if (!texture.loadFromFile(filename))
                std::cout << "Could not load " << filename << std::endl;

        // Sheet dimensions
        sheetWidth = texture.getSize().x;
        sheetHeight = texture.getSize().y;

        // Attach to player sprite
        sprite.setTexture(texture);

        // Set the position
        this->position.x = position.x;
        this->position.y = position.y;
        sprite.setPosition(this->position);

        // Get the sprite with and height
        spriteWidth = sheetWidth / columns;
        spriteHeight = sheetHeight / rows;

        // Set the source frame
        source.x = 0;
        source.y = Right;
}


void Player::update(sf::Event event)
{
        if (sf::Event::KeyPressed)
        {
                switch (event.key.code)
                {
                        case sf::Keyboard::Up:
                                source.y = Up;
                                break;

                        case sf::Keyboard::Down:
                                source.y = Down;
                                break;

                        case sf::Keyboard::Left:
                                source.y = Left;
                                break;

                        case sf::Keyboard::Right:
                                source.y = Right;
                                break;
                }
        }
}

void Player::draw(sf::RenderWindow &window)
{
        // Create rectangle in sheet around a sprite
        sprite.setTextureRect(sf::IntRect(source.x * spriteWidth, source.y * spriteHeight, spriteWidth, spriteHeight));

        // Draw the sprite
        window.draw(sprite);
}
 

Game.cpp
#include "Player.h"

int main()
{
        sf::RenderWindow window;

        // Initialize
        unsigned int width = 900;
        unsigned int height = 500;

        window.create(sf::VideoMode(width, height), "FPS");
        window.setFramerateLimit(60);
        window.setVerticalSyncEnabled(false);

        // Sprite stuff
        Player player("Sprites/Skeleton.png", 4, 9, sf::Vector2f(100, 100));

        // Game loop
        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                        }

                        player.update(event);
                }

                window.clear();
                player.draw(window);
                window.display();
        }
}

I am fairly new to C++, so sorry if there are any mistakes. I come from a Java background. Any help would be appreciated, thank you!

Pages: [1]