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

Author Topic: How to draw randomly for a game  (Read 3804 times)

0 Members and 1 Guest are viewing this topic.

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
How to draw randomly for a game
« on: September 17, 2013, 10:15:46 am »
Hi, as you have read by a lot of people. I'm working on a game. I'm just thinking how to pre-load a map for my game, so that I have say 600x600 pixels, the map is drawn with a texture 600x600 pixels with a texture of 16, I know how to get the texture made, just don't know how to draw more textures onto the screen.

I was thinking of maybe if SFML has the ability do window.draw(sprite,x,y); where x and y are the positions of the texture/tile. But that would take too long, so I was wondering if there is a easy way to do it.

Regards TheDianamu

Thanks in advance

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How to draw randomly for a game
« Reply #1 on: September 17, 2013, 10:21:58 am »
It's hard to tell what exactly you're asking for, but if you want to draw the same 16x16 texture at several places on the screen, it might be as simple as using Texture::setRepeated().  If that isn't what you want, maybe make a sprite with that texture, then for every location you want it change the sprite's x-y coordinates and draw it there.

By the way, sprites already have an x-y position, so a draw(sprite,x,y) command would be redundant.  Textures do not have a position (the sprites that use them do), which is part of the reason I can't understand what you're asking for.

Also I see no mention of anything random in your post, so why is "randomly" in the topic name?

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to draw randomly for a game
« Reply #2 on: September 17, 2013, 10:49:31 am »
The random post was placed, because I wanted to randomly generate spots of a certain tile/sprite in randomly chosen places.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How to draw randomly for a game
« Reply #3 on: September 17, 2013, 10:56:48 am »
Hmm, that sounds like you simply need to create a sprite for the texture, then have a loop where on each iteration you give the sprite a random position and then draw it.

Does any part of that not make sense?

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to draw randomly for a game
« Reply #4 on: September 17, 2013, 11:14:35 am »
That doe's not make any sense sorry. I'm just wondering am I calling the functions correctly and in the right spot? Because It keeps on breaking.

http://paste.ee/p/gEoWd


EDIT: I understand that I create the sprite to set the random in the loop.
« Last Edit: September 17, 2013, 11:21:54 am by TheDianamu »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How to draw randomly for a game
« Reply #5 on: September 17, 2013, 12:02:35 pm »
Well you appear to be recreating the window every single frame, so I'm not at all surprised that's causing problems.  In fact everything in viewControls() and gametextures() looks like it should only be happening once at the beginning.  Also, you wrote "gametexutres();" instead of "gametextures();".

It's starting to look like you don't really know what your own code is doing, so you might need to learn more about C++ and programming in general before we can really help you with this specific problem.

And by the way, NEVER say something as vague as "it keeps on breaking" when trying to get help.  Tell us exactly what it's doing or we won't have the slightest idea how to help you.

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to draw randomly for a game
« Reply #6 on: September 18, 2013, 01:27:33 pm »
Ok, I think I've done well, But now I'm getting problems with compiling with my calling the functions, when I compile. I get:

f:\lester\cwp\lester\lester\main.cpp(63) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(63) : error C2228: left of '.setTexture' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(63) : error C2065: 'player_left' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(64) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(64) : error C2228: left of '.move' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(68) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(68) : error C2228: left of '.setTexture' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(68) : error C2065: 'player_right' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(69) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(69) : error C2228: left of '.move' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(73) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(73) : error C2228: left of '.setTexture' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(73) : error C2065: 'player_up' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(74) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(74) : error C2228: left of '.move' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(78) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(78) : error C2228: left of '.setTexture' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(78) : error C2065: 'player_down' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(79) : error C2065: 'Player' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(79) : error C2228: left of '.move' must have class/struct/union
        type is ''unknown-type''
f:\lester\cwp\lester\lester\main.cpp(89) : error C2065: 'window' : undeclared identifier
f:\lester\cwp\lester\lester\main.cpp(89) : error C2228: left of '.isOpen' must have class/struct/union
 


My Code Is:

#include <SFML/Graphics.hpp>

void viewControls()
{
    sf::RenderWindow window(sf::VideoMode(800, 450), "Lester");

        sf::View gameView;
        gameView.reset(sf::FloatRect(200, 200, 300, 200));

        sf::View minimapView;
        minimapView.setCenter(sf::Vector2f(350, 300));
        minimapView.setSize(sf::Vector2f(200, 200));


        gameView.setViewport(sf::FloatRect(0, 0, 1, 1));

        minimapView.setViewport(sf::FloatRect(0.75f, 0, 0.25f, 0.25f));

}

void gametextures()
{
        //Game Textures
        sf::Texture grass;
        sf::Texture stone;
        sf::Texture flower_Yellow;
        sf::Texture flower_Red;
        sf::Texture sand;

        //Player Textures
        sf::Texture player_left;
        sf::Texture player_right;
        sf::Texture player_down;
        sf::Texture player_up;

        //Load All Textures
        grass.loadFromFile("grass.png");
        stone.loadFromFile("stone.png");
        flower_Yellow.loadFromFile("flower_yellow.png");
        sand.loadFromFile("sand.png");

        //Player Load
        player_up.loadFromFile("char_u.png");
        player_right.loadFromFile("char_r.png");
        player_left.loadFromFile("char_l.png");
        player_down.loadFromFile("char_d.png");

        //Set Textures to a Sprite
        sf::Sprite Grass(grass);
        sf::Sprite Stone(stone);
        sf::Sprite FlowerY(flower_Yellow);
        sf::Sprite Sand(sand);

        //Set Player Sprite
        sf::Sprite Player(player_up);
}

void playermovement(){
        gametextures();
        float playerSpeed = 2.0f;

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                Player.setTexture(player_left);
                Player.move(playerSpeed - 0.1, 0);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                Player.setTexture(player_right);
                Player.move(playerSpeed + 0.1, 0);
        }      

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                Player.setTexture(player_up);
                Player.move(0, playerSpeed - 0.1);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                Player.setTexture(player_down);
                Player.move(0, playerSpeed + 0.1);
        }
}


int main()
{
        viewControls();
        gametextures();

    while (window.isOpen())
    {
       sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                                window.close();
        }

                playermovement();

        window.clear(sf::Color::Black);
                window.draw(Player);
        window.display();
    }
    return 0;
}
 

If you could spot anything that I have done wrong, please tell me.

Thanks

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: How to draw randomly for a game
« Reply #7 on: September 18, 2013, 01:35:53 pm »
You really should get a C++ book and learn something about scope, your problem is not even related to SFML but to lacking C++ knowledge.

When you declare all of your textures/sprites inside a method they are only valid within that method and will be deleted at the end of that method. Not to mention with that method you load and delete textures every frame.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to draw randomly for a game
« Reply #8 on: September 18, 2013, 01:36:19 pm »
As the error message tells you, the Player variable is not declared in the scope where it is used.

You should really learn C++ basics, scopes are one of the most fundamental topics.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything