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.


Messages - Arcade

Pages: 1 ... 4 5 [6] 7 8 ... 16
76
Graphics / Re: How can I create global sprites?
« on: January 12, 2018, 04:00:17 pm »
That sentence you quoted was just to give you ideas on how to solve this:

However, I noticed that my code inside the int main() function was too messy, especially with those sprites' and textures' declarations "cramming" the function.

Instead of having something like this
sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    // error
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(...);
window.draw(sprite);
 

You can create an class that does all of that for you. Your main would just then be:
MyDrawableClass object;
window.draw(object);
 

However, designing your code like this leads to other things you need to consider. For example, if you simply store your texture in an object, then every time you create a new object of the same type you will be making unnecessary copies of that texture. Typically, you would create some kind of full blown "resource manager" class (or use the ones in libraries like "thor") for your textures and similar resources.

Basically, the topic of how to structure your code is a pretty big one with several different answers. It may be hard for people to give full explanations here on the forums without skimping on the details. Again, I recommend looking around the internet for typical game design patterns, or if you don't mind spending a small amount of money, buying one of the SFML game development books. I've actually never read the books myself, but I've heard that they can be a big help for beginners.

77
Graphics / Re: How can I create global sprites?
« on: January 04, 2018, 08:28:56 pm »
The point of my first paragraph wasn't about sprite specifically, but just to point out OP's misunderstanding that their only options were to create everything in the main function or as globals due to scope concerns. There are several other ways to organize code such that this is not the case. There are legitimate reasons why you may want to allocate textures dynamically, or custom classes that encapsulate your textures and sprites among other things.

Also, why would a sprite lose its reference to the texture when being copied into a vector? A sprite simply holds a pointer to its texture. As long as the texture doesn't move in memory you can copy the sprite as many times as you want and they will all point to the same texture just fine. Now, if a sprite's texture is in a vector you may run into trouble because in that case the texture can get moved and invalidate the sprite's pointer, but there is no problem with the sprite itself being in a vector.

78
Graphics / Re: How can I create global sprites?
« on: January 04, 2018, 04:38:40 pm »
Only memory that is allocated on the stack gets destroyed when going out of scope (such as a function ending). You can easily dynamically allocate memory on the heap using c++'s new operator that won't get automatically destroyed. In addition to this, you can organize your sprites and textures as members of C++ classes or structs to keep things from getting "messy". Those classes and structs themselves can be allocated on the heap if need be. Or maybe std::vector would work for your use case.

You may want to spend a little more time learning C++ and the standard library to fully understand what your organizational options are. From there, you might consider reading through one of the SFML game design books to get an idea on how you can structure the logic for a game.

In general you don't want to use SFML classes like sf::Texture as global variables. Some SFML classes depend on other global variables themselves for technical reasons. In C++ the order of destruction of global objects is undefined across translation units. That means if you have 2 global variables that depend on each other, such as the case with a global sf::Texture, the wrong one might get destructed first and you end up with undefined behavior (possibly a crash).

79
General / Re: Set Position Of Sprite Depending On Rotation
« on: January 03, 2018, 10:45:53 pm »
It looks like your bullet isn't moving at all. It is simply rotating around its origin (top left), but its starting position isn't moving as the character rotates. Am I interpreting the attached picture correctly? If not, perhaps you can explain more exactly what the bad behavior you are seeing is.

In the last line of the code I provided, you end up with a variable called bulletPosition. You need to actually set the bullet's sprite position to that value. Did you do that?

If you still can't figure out your problem, perhaps you could show a more complete (but minimal) example of how you are setting your character and bullet positions and rotations. It's hard to guess what could be going wrong without seeing code.

80
General / Re: Set Position Of Sprite Depending On Rotation
« on: January 02, 2018, 11:52:30 pm »
How far off is the perceived origin? You can probably simplify your code to something like the following (I'm not able to test anything at the moment, so sorry about possible mistakes):

sf::Vector2f bulletOffset(15, -80);
               
sf::Transform tansform;
tansform.rotate(character.getRotation());
sf::Vector2f newBulletOffset = tansform.transformPoint(bulletOffset);

sf::Vector2f bulletPosition = character.getPosition() + newBulletOffset;
 

Also, make sure the origin of your bullet is set to a good value (probably should be the bottom middle), and that the numbers 15 and -80 are correct.

81
General / Re: Set Position Of Sprite Depending On Rotation
« on: December 29, 2017, 06:40:40 pm »
Look at the sf::Transform documentation and scroll down to the "Detailed Description" section for an example of how to use it. You are basically missing the step where you pass your point into the transform so that it can be rotated.

Since you are creating a point that has an absolute position (instead of one relative to your character), you may also want to look into using one of the other rotate() overloads that allow you to pass in the center of rotation (which would be the character's origin).

82
General / Re: Set Position Of Sprite Depending On Rotation
« on: December 28, 2017, 07:04:44 pm »
You said you had no luck trying to use trig, but could you be more specific? What did you try and what wasn't working?

Basically you just need to calculate a relative vector from the origin of your character sprite to the end of the gun, similar to what you have already done in the code you showed us. Then, rotate that vector based on the character's sprite rotation. You can use SFML's Transform class for this if you want to make things easier.

84
General / Re: setViewport squashing textures
« on: December 20, 2017, 12:02:08 am »
If a view is larger than the viewport, then everything has to get squeezed to a smaller size in order to fit. You are setting your view size to be the same as the default view (the size of the whole screen), but then making the viewport smaller. You need the size of the view and the viewport to match if you don't want any scalling to happen.

85
SFML projects / Re: Screenshot Thread
« on: December 19, 2017, 06:50:56 am »
I've recently started working on a (American) football game. The gif shows a few features of the "play designer" tool where I can design, simulate, and save plays for each team to run. The next step is to improve upon the players' AI.


86
General / Re: TDS Shooter Enemy Programming issues
« on: December 15, 2017, 11:32:13 pm »
To expand on fallahn's answer a bit, the best solution depends on what kind of game you are making and how smart the enemies should be. If you don't want your enemies to end up in a line then you will need to implement something a little more advanced than just taking the direct angle towards the player.

For example, if you need your enemies to be kind of dumb (zombies?) then you can just give them a random "reaction delay" such that they don't react instantly to a player's position. Different enemies can have different delays so that they don't end up all moving together. Or maybe they don't take the perfect angle towards the player.

If you need your enemies to be smart you can try to predict where the player is moving to and try to cut them off. Or, as fallahn mentioned, you can try to implement a more advanced "flocking" behavior for your group of enemies.

You can google these terms and probably find good resources on how to implement different types of moving AIs. These aren't really SFML related questions, so you may not get very detailed help here.

87
General / Re: Circe Shape only moves when variable change or key press?
« on: December 13, 2017, 12:53:40 am »
This code is quite hard to read through. It may be useful to spend more time learning C++ and object oriented programming before diving into a library like SFML. Specifically, it seems odd that you are creating and destroying your "ship" object every loop of your game and keeping track of its state using static members.

To try and directly answer your question, though, it looks like your problem is that you are treating AccelX and AccelY as positions and not accelerations. Meaning, you are only changing those values when a key is pressed, and then using those values directly to set the position of your circle.

Also, to clarify, it sounds like what you are actually wanting is a way to modify your ship's Velocity (not Acceleration). The basic concept is that you will need 2 variables, a Position and a Velocity. Every loop of your game you should add Velocity to Position and set your circle to that updated Position. You would then increase or decrease Velocity whenever UP or DOWN is pressed.

Ship::PlayerShip.setPosition(+Ship::AccelX, +Ship::AccelY);
 

This is just a guess, but this line tells me you might be on the right track, but just don't know correct C++ syntax. I'm not sure what you're expecting those  '+'  signs to do.

88
Graphics / Re: text from string is not visible any more
« on: December 06, 2017, 10:44:17 pm »
So, basically you changed this
if(!font.loadFromFile("Tools/PIXEARG_.TTF")){
        std::cout<<"[MainWindow:Error] font not found" << std::endl;
    }
 

to this?
fontLoader(fontMenu);
 

What is the definition of fontLoader? Are you passing fontMenu by value instead of by reference?

89
General discussions / Re: SFML Game Development by Example - 4th SFML book
« on: November 30, 2017, 08:29:56 pm »
I haven't bought or read through the book, but it seems likely that those numbers represent the position of those values within their corresponding enum. KeyPressed is equal to 5 in enum sf::Event::EventType (start counting from 0), and F5 is likely equal to 89 in
enum sf::Keyboard::Key


90
Graphics / Re: Unwanted extra drawing at box2d bodydef locations
« on: November 15, 2017, 09:26:25 pm »
I don't know anything about Box2D, but looking at the code
for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
{
   b2Vec2 pos = it->GetPosition();
   float angle = it->GetAngle();
   sBall.setPosition(pos.x * SCALE, pos.y*SCALE);
   sBall.setRotation(angle * DEG);
   window.draw(sBall);
}
 

Won't iterating over World.GetBodyList() give you 4 objects (3 walls and 1 ball)? Which means for each object, including the walls, you are moving the ball sprite to its location and drawing it. Seems pretty straightforward, but I may be misunderstanding something.

Pages: 1 ... 4 5 [6] 7 8 ... 16
anything