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

Pages: [1]
1
General / Moving character
« on: June 10, 2012, 03:31:41 pm »
Hello,

until now I have created few simple games like tictactoe, pong, brake it. But now I want to create something more interesting.

So I got idea about some game.
In this game I have a knight that is moving around (left and right).
So I am interesting how is it done?
 Do I create .gif with knife moving around and render it ? Or I make lets say 30 different images and load it one after another to get it look like it is moving ? And how do I get collision detection for lets say knight's sword with enemy unit?

I tried to search it but it didn't help.
I would appreciate if someone could explain this to me.

2
Graphics / Re: Passing sf::RenderWindow to multiple .cpp files
« on: May 30, 2012, 02:16:17 pm »
Some things may be lost in translation so you didn't understood me well.
Anyway I found what I was looking for (two ways):
-reference
-using external methods
Ty for replays.

3
Graphics / Passing sf::RenderWindow to multiple .cpp files
« on: May 29, 2012, 06:08:23 pm »
Hello,
I have started to create some application witch have a lots of functions. So I wanted to group out that functions in multiple .cpp files, but problem is that in every function there is some drawing so when it is in other .cpp file I cannot draw on window that I have rendered in my main.cpp. Is it possible to pass reference or window handle to function in other .cpp file ? I tried with sf::Window::getSystemHandle() but couldnt make it out.
Is there any other solution ?

4
SFML projects / Re: TicTacToe 2Players
« on: May 29, 2012, 03:40:28 pm »
I have fixed it adding goto: when sfml window is closed.

Oh god, this would take some explaining... Let's just put it this way, goto bad. Especially if you're a new programmer.

It's something you should just condition yourself to avoid by impulse and be done with. There was a lengthy debate about the pros and cons about thirty years ago, the general consensus was "no unless you're being really clever" and it's faded from modern usage almost entirely (with most modern languages not even featuring a goto command).
I used goto because there is 3 while loops in there and no other way of exiting 3 of them at once since break just brakes one while. If you know some other solutions of exiting 3 while (or for) loops at once share please I had similar problems before.
The problem with goto, is generally no respect for SESE and such things.
I'll change what MorleyDev said. It's not bad. It's EVIL when you begin.

Now for your code:
You mind explaining the way you did your include? Only SFML/Graphics.hpp and SFML/Audio.hpp are needed, since Graphics include all the other.

Globals are almost as evil as goto. What you did is C using C++ syntax. You could, and should, try to englobe the game into a class. The only global in this case that could be ok, are the const int for the size. Plus except board[][](and possibly your clock, that can be fixed with what I say after), there is not an object you use in the other functions. And if you ever needed them, you would have passed them by references.

The way you calculate the framerate is not the best(not even sure it's really precise).
You could just go with something like that:
return IntToString(static_cast<int>(1.f/elapsed.restart().asSeconds()));
I'll be picky here, but you could just use std::ostringstream. It indicates that it's only that way you'll be using, also a bit faster(but it's optional, just me going crazy with those little thing).

Your pause function is useless. You could just do:
sf::sleep(sf::Seconds(2.5f));

You still have your infinite loop and you "solved" the problem with a goto. Don't. The game loop you need is already there with window.isOpen(). Remove the while(1) and the goto.

The way you handle mouse click looks good though.

You should  consider spacing out your code and commenting more. Even if you don't intend to show your code. It's always usefull to see why you did thing and the reason for some code. Later, when you'll look back at your code, you will find those comments very usefull.

Last thing. I find that the code looks very much beginner. Not that it's bad. We all started somewhere and produced bad code, and we pretty much all do it from times to times(or is it just me?). But you should consider practicing your C++ before going into games. Things like Class, standard containers(std::array, std::vector), templates could help you.

I dont know why but strange thinks happened to me after I installed SFML to my computer. Creating debug and release libraries went fine, linking them also went fine but when I tried to compile some simple code I was unable because that code contained sf::Event class and editor was unable to find that class. (I included SFML/Graphics). I have used google for few days, posted problem here-noting helped. And than one day I got idea to do :
#include <SFML/Window/Event.hpp>
and it helped, since than I have to do it all like this.
You are right about globals but as I wrote it is my first app with sfml and I didnt write it to be clean just to see how classes in sfml works, so I wasnt paying any attention to it.
My goal wasnt to make a game - it was just to see how things work so I didnt use any classes.
I had some problems with sf::sleep coulnt make it to work so I put it like this but now I see how it is done :D
If I remove infinity while it wouldnt be able to play more games because  1 game (board gets full,score is counted) is in window.isOpen() and while(1) repeats that action. So instead of playing one game and exit application and enter again you can do it like this.
Idea about commenting code is good but I did int in few hours (I knew i wont leave it for tomorrow) and had all that stuff in my head and too lazy to comment :) .
I know a lot about c++ because I have learning it in school for 1 year now. We did OOP, STL and templates but I repeat again my goal wasnt to create a game, it was just to see how stuff in SFML works.
Ty for your replays it was useful.

5
Graphics / Re: Getting object from picture, not background
« on: May 28, 2012, 07:03:02 pm »
Ty for replays.
sf::Image::createMaskFromColor(sf::Color::White) worked great!
And editing image is also easier and maybe better solution than createMaskFromColor.
Ty anyway it helped a lot!

6
SFML projects / Re: TicTacToe 2Players
« on: May 28, 2012, 05:48:58 pm »
Sounds are...interesting.
But you've distributed the debug version, you should have distributed the release one.
Also you forgot to give OpenAL32.dll and libsndfile-1.dll for the sound.
You also gave all dll, release and debug. The network dll is not use, so why giving it?
I didn't look much at the code, but why are you doing an infinite loop? It causes the game to never close when I close the game window. I have to close the console window myself, not something I expect.
I'll take a deeper look at the code later, and possibly post my thoughts on it(good and bad).
Other than that, the game still is clean and sweet.

Ty for your reply and comments.
I have never "release" game so it is kind new to me so ty for saying what I need to do next time.
Infinity loop is there to not allow game to close when 1 game is played-you can play as many games as you want, well 20 at the moment :).
I have fixed it adding goto: when sfml window is closed.
If you replay with thoughts about code I will be grateful to you.

Is this how it should look like :
http://www.2shared.com/file/0QXNhbSr/TicTacToe.html

7
Graphics / Getting object from picture, not background
« on: May 27, 2012, 08:31:56 pm »
Hello,
I am still newbe in SFML since I started like a day ago :D .
So far I created TicTacToe game you can see it here.
Now I moved on Pong game and I have a problem.
In this game I have ball picture it is simple ball with white background. But my window background is other picture (green color) that I found on google. So when I draw ball on that background I saw rectangle - the whole image that ball is.
I just want to make ball visible not background
Is there any way of achieving this ?

8
SFML projects / TicTacToe 2Players
« on: May 27, 2012, 03:51:54 pm »
My first project with SFML 2.0 I just wanted to make something simple to see how stuff works with SFML.
Done in in 3,5 hours.



Download link: http://www.2shared.com/file/JvIyLmij/TicTacToe.html
#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/System/Sleep.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML\Audio\Sound.hpp>
#include <iostream>
#include <sstream>
#include <string>
sf::Time thetime;
sf::Clock elapsed;
std::string IntToString(int d)
{
   std::stringstream ss;
   ss << d;
   return ss.str();
}

std::string GetFrameRate()
{    static int frameCounter = 0;
      static int fps = 0;
      frameCounter++;
      thetime = elapsed.getElapsedTime();
      if(thetime.asMilliseconds() > 999)
      {
         fps = frameCounter;
         frameCounter = 0;
         elapsed.restart();
      }

      return IntToString(fps);
}
const int SCREEN_WIDTH=649;
const int SCREEN_HEIGHT=460;
const int SCREEN_BPP=32;
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP),"TicTacToe",!sf::Style::Resize|sf::Style::Close);
char *score[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
enum BoardValues{x,o,empty};
BoardValues board[3][3];
BoardValues onPlay;
BoardValues winnerCheck()
{
        if (board[0][0]==x && board[0][0]==board[1][1] && board[1][1]==board[2][2]) {return x;}
        if (board[2][0]==x && board[2][0]==board[1][1] && board[1][1]==board[0][2]) {return x;}
        if (board[0][0]==x && board[0][0]==board[1][0] && board[1][0]==board[2][0]) {return x;}
        if (board[0][1]==x && board[0][1]==board[1][1] && board[1][1]==board[2][1]) {return x;}
        if (board[0][2]==x && board[0][2]==board[1][2] && board[1][2]==board[2][2]) {return x;}
        if (board[0][0]==x && board[0][0]==board[0][1] && board[0][1]==board[0][2]) {return x;}
        if (board[1][0]==x && board[1][0]==board[1][1] && board[1][1]==board[1][2]) {return x;}
        if (board[2][0]==x && board[2][0]==board[2][1] && board[2][1]==board[2][2]) {return x;}
        if (board[0][0]==o && board[0][0]==board[1][1] && board[1][1]==board[2][2]) {return o;}
        if (board[2][0]==o && board[2][0]==board[1][1] && board[1][1]==board[0][2]) {return o;}
        if (board[0][0]==o && board[0][0]==board[1][0] && board[1][0]==board[2][0]) {return o;}
        if (board[0][1]==o && board[0][1]==board[1][1] && board[1][1]==board[2][1]) {return o;}
        if (board[0][2]==o && board[0][2]==board[1][2] && board[1][2]==board[2][2]) {return o;}
        if (board[0][0]==o && board[0][0]==board[0][1] && board[0][1]==board[0][2]) {return o;}
        if (board[1][0]==o && board[1][0]==board[1][1] && board[1][1]==board[1][2]) {return o;}
        if (board[2][0]==o && board[2][0]==board[2][1] && board[2][1]==board[2][2]) {return o;}
        return empty;
}
bool tie()
{
        for(int i=0;i<3;i++)
        {
                for(int j=0;j<3;j++)
                {
                        if (board[i][j]==empty)
                                return false;
                }
        }
        return true;
}
inline void pause()
{
                sf::Time forSleep;
                forSleep=sf::seconds(2.5f);
                sf::sleep(forSleep);
}
int main()
{
        int xScore=0,oScore=0,tieScore=0;
        sf::Text TtieScore(score[tieScore],sf::Font::getDefaultFont(),70);
        TtieScore.setColor(sf::Color::Black);
        TtieScore.setPosition(515,140);
        sf::Text TxScore(score[xScore],sf::Font::getDefaultFont(),70);
        TxScore.setColor(sf::Color(2,0,153));
        TxScore.setPosition(515,35);
        sf::Text ToScore(score[oScore],sf::Font::getDefaultFont(),70);
        ToScore.setColor(sf::Color(205,0,0));
        ToScore.setPosition(515,300);
        while(1)
        {
        for(int i=0;i<3;i++)
        {
                for(int j=0;j<3;j++)
                {
                        board[i][j]=empty;
                }
        }
        sf::Font font;
        if(!font.loadFromFile("ABITE.ttf"))
                return 1;
        sf::Text TxWin("X won",font,70);
        TxWin.setColor(sf::Color::Black);
        TxWin.setPosition(200,200);
        sf::Text ToWin("O won",font,70);
        ToWin.setColor(sf::Color::Black);
        ToWin.setPosition(200,200);
        sf::Text Ttie("Tie",font,70);
        Ttie.setColor(sf::Color::Black);
        Ttie.setPosition(200,200);
        sf::Text TtieText("Tie",font,25);
        TtieText.setColor(sf::Color::Black);
        TtieText.setPosition(450,132);
        sf::Text TplayerX("X Player",font,25);
        TplayerX.setColor(sf::Color (2,0,153));
        TplayerX.setPosition(450,25);
        sf::Text TplayerO("O Player",font,25);
        TplayerO.setColor(sf::Color(205,0,0));
        TplayerO.setPosition(450,290);
        window.setFramerateLimit(50);
        sf::SoundBuffer BXsound;
        if(!BXsound.loadFromFile("Xsound.wav"))
                return 1;
        sf::Sound Xsound;
        Xsound.setBuffer(BXsound);
        sf::SoundBuffer BOsound;
        if(!BOsound.loadFromFile("Osound.wav"))
                return 1;
        sf::Sound Osound;
        Osound.setBuffer(BOsound);
        sf::SoundBuffer BTieSound;
        if(!BTieSound.loadFromFile("TieSound.wav"))
                return 1;
        sf::Sound TieSound;
        TieSound.setBuffer(BTieSound);
        sf::Texture Tbackground;
    sf::Texture Tx;
    sf::Texture To;
        if(!Tbackground.loadFromFile("pozadina.jpg") || !Tx.loadFromFile("iks.jpg") || !To.loadFromFile("oks.jpg"))
                return 1;
        sf::Sprite Sbackground;
        sf::Sprite Sx;
        sf::Sprite So;
        Sx.setTexture(Tx);
        So.setTexture(To);
        Sbackground.setTexture(Tbackground);
        Sbackground.setPosition(0,0);
        sf::Sprite SonPlay;
        sf::Sprite Sfield[9];
        bool mouseClicked[9]={false,false,false,false,false,false,false,false,false};
        int turn=1;
        while(window.isOpen())
        {      
                window.setTitle("TicTacToe FPS:" + GetFrameRate());
                if(turn%2==0)
                {
                        SonPlay=So;
                        onPlay=o;
                }
                else
                {
                        SonPlay=Sx;
                        onPlay=x;
                }
                 sf::Event Event;
         while (window.pollEvent(Event))
         {
       
             if (Event.type == sf::Event::Closed)
                 window.close();
         }
                 //input
                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                 {
                         sf::Vector2i mousePosition=sf::Mouse::getPosition(window);
                         if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[0]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[0]=true;
                                 Sfield[0]=SonPlay;
                                 Sfield[0].setPosition(10,10);
                                 turn++;
                                 board[0][0]=onPlay;

                         }//1,1 polje

                         if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[1]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[1]=true;
                                 Sfield[1]=SonPlay;
                                 Sfield[1].setPosition(161,10);
                                 turn++;
                                 board[0][1]=onPlay;
                         }//1,2 polje

                         if(mousePosition.x<414 && mousePosition.x>314 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[2]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[2]=true;
                                 Sfield[2]=SonPlay;
                                 Sfield[2].setPosition(314,10);
                                 turn++;
                                 board[0][2]=onPlay;
                         }//1,3 polje

                         if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[3]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[3]=true;
                                 Sfield[3]=SonPlay;
                                 Sfield[3].setPosition(10,161);
                                 turn++;
                                 board[1][0]=onPlay;
                         }//2,1 polje

                         if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[4]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[4]=true;
                                 Sfield[4]=SonPlay;
                                 Sfield[4].setPosition(161,170);
                                 turn++;
                                 board[1][1]=onPlay;
                         }//2,2 polje

                         if(mousePosition.x<414 && mousePosition.x>341 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[5]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[5]=true;
                                 Sfield[5]=SonPlay;
                                 Sfield[5].setPosition(319,166);
                                 turn++;
                                 board[1][2]=onPlay;
                         }//2,3 polje

                         if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[6]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[6]=true;
                                 Sfield[6]=SonPlay;
                                 Sfield[6].setPosition(10,322);
                                 turn++;
                                 board[2][0]=onPlay;
                         }//3,1 polje

                         if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[7]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[7]=true;
                                 Sfield[7]=SonPlay;
                                 Sfield[7].setPosition(161,322);
                                 turn++;
                                 board[2][1]=onPlay;
                         }//3,2 polje

                         if(mousePosition.x<414 && mousePosition.x>314 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[8]==false)
                         {
                                 if(onPlay==x)
                                         Xsound.play();
                                 else if (onPlay==o)
                                         Osound.play();
                                 mouseClicked[8]=true;
                                 Sfield[8]=SonPlay;
                                 Sfield[8].setPosition(314,322);
                                 turn++;
                                 board[2][2]=onPlay;
                         }//3,3 polje

                 }
                 window.clear(sf::Color(250,250,250));
                 if (winnerCheck()==x)
                 {
                         xScore++;
                         TxScore.setString(score[xScore]);
                         window.draw(TxWin);
                         window.display();
                         Xsound.play();
                         pause();
                         break;
                 }
                 else if(winnerCheck()==o)
                 {
                         oScore++;
                         ToScore.setString(score[oScore]);
                         window.draw(ToWin);
                         window.display();
                         Osound.play();
                         pause();
                         break;
                 }
                 else if (tie()==true)
                 {
                         tieScore++;
                         TtieScore.setString(score[tieScore]);
                         window.draw(Ttie);
                          window.display();
                          TieSound.play();
                         pause();
                         break;
                 }
                 window.draw(Sbackground);
                 for(int i=0;i<9;i++)
                         window.draw(Sfield[i]);
                 window.draw(TplayerX);
                 window.draw(TxScore);
                 window.draw(TplayerO);
                 window.draw(TtieScore);
                 window.draw(TtieText);
                 window.draw(ToScore);
                 window.display();
        }
        }
        return 0;
}
What do you think?
Am I doing something wrong?
If you have time check it out :)

PS.
Turn your speakers   :D

9
General / Re: Problem with sf::Event
« on: May 25, 2012, 12:00:31 pm »
I found what is wrong.
Lock thread.

If you are interested or have this or similar problem all you have to do is
#include <SFML/Window/Event.hpp>

10
General / Re: Problem with sf::Event
« on: May 25, 2012, 11:06:31 am »
Already did all stuff that you wrote - nothing helped. And yes I followed tutorial.
But when I downloaded SFML 2.0 snapshot and extract it at the end it give me some error :
!   C:\Users\NAME\Desktop\LaurentGomila-SFML-2.0-rc-24-gac9bda5.tar.gz: Cannot open Versions\Current\Resources (LaurentGomila-SFML-ac9bda5\extlibs\libs-osx\Frameworks\sndfile.framework\Resources --> Versions\Current\Resources)
!   C:\Users\NAME\Desktop\LaurentGomila-SFML-2.0-rc-24-gac9bda5.tar.gz: Symbolic link points to missing file
 
I have just ignored but now it come on my mind.
Could this be problem ?

11
General / Re: Problem with sf::Event
« on: May 25, 2012, 09:51:06 am »
I had a problem setting it and solution that worked for me is with CMake and SFML 2.0 snapshot then build debug and release in msvc++, then link them with project and put all dll's into folder with .exe file.
I dont know is it 2rc.

12
General / Problem with sf::Event
« on: May 24, 2012, 08:15:14 pm »
Hello,
I have just started using sfml 2.0. I set up everything, copy some code, paste it when I try to run i got this error :
Error   1       error C2079: 'Event' uses undefined class 'sf::Event'   c:\users\NAME\documents\visual studio 2010\projects\sfml test application\sfml test application\main.cpp        12
 
Problem is about sf::Event and I cannot do much without that class.
Here is print screen:

Do you know what is the problem.
ps.
I am not shure is this topic in right section.
pss.
Search didnt help about this.

Pages: [1]