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

Pages: [1]
1
General / Collision between mouse and convex shape
« on: June 22, 2015, 04:41:23 pm »
I'd like to detect collision between mouse and convex shape, but I have no idea how can I do that. I need a pixel-perfect collision, so getGlobalBounds() method won't do here. I need to know if the mouse is inside my convex shape or not. What's the best way to achieve that?

2
Graphics / Nothing is drawn (inheriting from Drawable)
« on: April 10, 2015, 01:35:26 am »
Hello,

I'm starting this little project and I started with writing a Room class. Room is a single board that we see in the game, like a street, a lab room. Room consists of layers. This should allow me to do some nice work with z order. I don't know how many layers each room will have - one could have only 2 layers, second 5 layers and third 3 layers. So creating a simple array with a constant number of elements wouldn't do the trick, it would be useless consuming too much memory. Therefore I thought about vector. But personally I don't really like vector, it also can consume much memory. I decided to create my own easy template class for some dynamic array. I did that and I think it works okay right now (although there are some things that I could improve, but that's not a matter of my problem). Once I created my dynamic array class I, of course, began writing Room class. I started with a constructor, deconstructor and a method for adding layers. I've done drawing by making Room class inherit from Drawable (so in the game loop I can do something like this: window.draw(myroom)). I overrode draw method correctly, I think... And so, I created a Room object and I added one layer to it, pressed F7, then F5 and... Nothing is drawn. There's only a white sprite in the place where my background should be visible. This white sprite's position is the same as the position given in the parameter in the add layer function, so I think it kinda works... Something there works, but something doesn't. And I have no idea what doesn't. Here's the complete code I have (it's not long):

DArray.h
Code: [Select]
#ifndef DARRAY_H
#define DARRAY_H

#include <iostream>

using namespace std;

template<class Foo>
class DArray {
private:
struct node {
Foo data;
node* ptr;

node(Foo d) {
data = d;
ptr = NULL;
}
};
node* first;
int counter;

public:
DArray();
~DArray();
void push(Foo d);
Foo pop();
int size() const;
bool empty();
Foo& operator[](int i) const;
};

template<class Foo>
DArray<Foo>::DArray() {
first = NULL;
counter = 0;
}

template<class Foo>
DArray<Foo>::~DArray() {
while (!empty())
pop();
}

template<class Foo>
void DArray<Foo>::push(Foo d) {
if (empty()) {
first = new node(d);
}
else {
node* tmp = first;
while (tmp->ptr != NULL)
tmp = tmp->ptr;
tmp->ptr = new node(d);
}
counter++;
}

template<class Foo>
Foo DArray<Foo>::pop() {
if (!empty()) {
Foo d;
node* tmp;
tmp = first;
first = first->ptr;
d = tmp->data;
delete tmp;
return d;
}
}

template<class Foo>
int DArray<Foo>::size() const {
return counter;
}

template<class Foo>
bool DArray<Foo>::empty() {
if (first == NULL)
return true;
else
return false;
}

template<class Foo>
Foo& DArray<Foo>::operator[](int i) const {
node* tmp = first;

for (int j = 1; j < i+1; j++) {
tmp = tmp->ptr;
}

return tmp->data;
}

#endif

Room.h
Code: [Select]
#ifndef ROOM_H
#define ROOM_H

#include <SFML\Graphics.hpp>
#include "DArray.h"

using namespace sf;
using namespace std;

struct Layer {
Texture tex;
Sprite spr;
//id, name

Layer() { }

Layer(const string path, Vector2f pos) {
tex.loadFromFile(path);
spr.setTexture(tex);
spr.setPosition(pos);
}
};

class Room : public Drawable {
private:
DArray<Layer> layers;
//int id;
//string name;

virtual void draw(RenderTarget& target, RenderStates states) const {
for (int i = 0; i < layers.size(); i++) {
target.draw(layers[i].spr, states);
}
}

public:
Room();
~Room();
void AddLayer(const string path, Vector2f pos);
//void RemoveLayer();
//show layers' names
};

#endif

Room.cpp
Code: [Select]
#include "Room.h"

Room::Room() {

}

Room::~Room() {

}

void Room::AddLayer(const string path, Vector2f pos) {
layers.push(Layer(path, pos));
}

GameApp.h
Code: [Select]
#ifndef GAMEAPP_H
#define GAMEAPP_H

#include <SFML\Graphics.hpp>
#include <iostream>
#include "Room.h"

using namespace sf;
using namespace std;

class GameApp {
private:
RenderWindow win;
bool fullscreen;
Event evt;
double dt, dwticks, dwnewticks;
Clock main_clock;

public:
GameApp();
~GameApp();
void Run();
//void ToggleFullscreen();
};

#endif

GameApp.cpp
Code: [Select]
#include "GameApp.h"

GameApp::GameApp() {
fullscreen = false;
dt = dwticks = dwnewticks = 0.f;

win.create(VideoMode(800, 600), "PNC", fullscreen ? Style::Fullscreen : Style::Default);
win.setFramerateLimit(60);
win.setVerticalSyncEnabled(true);
win.setMouseCursorVisible(true);
}

GameApp::~GameApp() {

}

void GameApp::Run() {
main_clock.restart();
dwticks = main_clock.getElapsedTime().asMilliseconds();

Room str;
str.AddLayer("data/rooms/str/gfx/bgr.jpg", Vector2f(0, 50));

while (win.isOpen()) {
if (win.pollEvent(evt)) {
if (Keyboard::isKeyPressed(Keyboard::Key::Escape) ||
evt.type == Event::Closed) {
win.close();
}
}
else {
dwnewticks = main_clock.getElapsedTime().asMilliseconds();
dt = dwnewticks > dwticks ? (dwnewticks - dwticks) / 4000.f : 0.f;
dwticks = dwnewticks;

win.clear(Color(0, 0, 0));
win.draw(str);
win.display();
}
}
}

main.cpp
Code: [Select]
#include "GameApp.h"

int main() {
GameApp game;

game.Run();

return 0;
}

When I replaced the const string path parameter in Layer constructor with Texture& t (and also did the same changes in add layer method) and then in the Run() method in GameApp class I created a texture:

Code: [Select]
Texture t;
t.loadFromFile("data/rooms/str/gfx/bgr.jpg");

and passed it to the parameter as this:

Code: [Select]
Room str;
str.AddLayer(t, Vector2f(0, 50));

it did the trick. Background shows correctly that way. But I want it to work also with that string parameter.

I'd be forever ashamed if I did something really stupid.

3
Graphics / Font with outline is transparent
« on: December 18, 2014, 08:32:26 pm »
Hi there,

I downloaded this font: http://www.1001freefonts.com/snacker_comic.font. It is supposed to be white with black outline. But in my project I get this instead:
http://i.imgur.com/iLoo8ko.png
You see, outline is ok, it is black. But the rest of the font color is transparent and I don't know why. I saw that I have the same problem in Paint - this font just won't work properly.

In my project I have two variables, a font and a text, I load and set them as this:
//there's using namespace sf somewhere up there

Font snacker;
Text example;

snacker.loadFromFile("data/fonts/snacker.ttf");

example.setFont(snacker);
example.setCharacterSize(26);
example.setColor(Color::Black);
example.setStyle(Text::Regular);
example.setPosition(Vector2f(630, 360));
example.setString("Pizza Box");

Is there any chance that this font would work?

4
System / Smoothing mouse movement
« on: October 19, 2014, 06:07:42 pm »
Hi,

I'd like to move a sprite by moving a mouse. First, I decided to use a setPosition() method:

Code: [Select]
double mx, my;
mx = my = 0.f;

...

//in main loop
mx = Mouse::getPosition().x;
my = Mouse::getPosition().y;

mysprite.setPosition(mx, my);

And it actually did the trick, but it's definitely not the perfect solution. Because Mouse::getPosition() returns Vector2i the movement is not smooth and you can clearly see that. So I did this:

Code: [Select]
double mx, my, lastMX, lastMY;
mx = my = lastMX = lastMY = 0.f;

...

//in main loop
mx = Mouse::getPosition().x;
my = Mouse::getPosition().y;

mysprite.move((mx - lastMX), (my - lastMY));

lastMX = Mouse::getPosition().x;
lastMY = Mouse::getPosition().y;

It's not bad, the movement is smoother, but still I think it can be done better. I tried to multiply (mx - lastMX) and (my - lastMY) by deltatime and some constant speed, but that was... Ugh, not good. I'm out of ideas. What can I do to make a mouse movement smooth?

5
General / Grid similar to that found in editors
« on: August 27, 2014, 06:42:42 pm »
Hi,

I'm writing a program and I was wondering how to make a grid just like that which can be found in all sort of editors (UDK, UE4, 3d modelling apps). A grid which responds to mouse - which can be moved by dragging a mouse and can be scaled when we move mouse wheel. Any ideas how to do that?

6
I have a class that has Text and Font variables:

Code: [Select]
using namespace sf;

class MyClass {
private:
   Font times;
   Text song_name;

public:
   MyClass();
   ~MyClass();
   void DrawMethod(RenderWindow&);
};

In constructor I load font, I set song_name font, song_name character size, color - that works fine. But when I try to use setString I get an unhandled exception error.

Code: [Select]
MyClass::MyClass() {
   times.loadFromFile("C:/Windows/Fonts/times.ttf");

   song_name.setFont(times);
   song_name.setCharacterSize(16);
   song_name.setColor(Color::White);
   song_name.setStyle(Text::Bold);

   //***Above works!***
   //But this doesn't:
   song_name.setString("and here's some text");
}

Same happens when I try to draw my text:

Code: [Select]
void MyClass::DrawMethod(RenderWindow& win) {
   win.draw(song_name);
}

What's happening?

7
Audio / How can I know if the song ended or not?
« on: June 08, 2014, 01:15:58 am »
Let's say I have a Music object and I load a certain song to it:

Code: [Select]
sf::Music song;
song.openFromFile("path.wav");
song.play();

I want to know if a song ended playing or not. If it did I want to load another song. I tried doing something like this:

Code: [Select]
if (song.getStatus() == sf::Music::Status::Stopped) {
   song.openFromFile("anothersong.wav");
   song.play();
}

But it doesn't work and the problem seems to be with the condition. This one doesn't say me if the song ended. When the song ends status should be set to stopped, right? I thought so and I thought that my code would work, but it doesn't. I tried several things (I thought about checking song duration, and counting time) and I checked if getStatus() method returns the right value (when I press "S" the song stops playing and its status is set to Stopped, when I press "P" the song starts playing and its status is set to Playing - that works fine, but seems that when the song ends status is still set to Playing). How can I check if song ended playing?

8
Graphics / Sprite's texture doesn't change properly
« on: November 27, 2013, 07:27:15 pm »
Hi,

I have two classes - Graph and Node. In Graph class I declare an array of Nodes. In the Update method of class Graph I have a loop for checking each node... So, I have 25 nodes. At the beginning, each node has a texture "NOTVISITED". If I click on a node it should change its texture to "CLICKED". When I click on another node, my first node changes its texture to "VISITED" and the now-clicked one changes texture to "CLICKED". That works ok, but there's one problem. Namely, if there's a node with a texture "CLICKED" and I click on another node, first node changes texture to "VISITED", but just after I do some mouse movement or I click again... And it should change its texture to "VISITED" right after clicking on another node. I'm just learning coding 2d apps and I have no idea how to fix it. Could someone help me?

Here's the code of Graph::Update:

Code: [Select]
for (int i=0; i<size; i++) {
        if (gm.MouseClicked(w[i].getSpr(), win, ev)) { //win - window, ev - event
            for (int j=0; j<size; j++) {
                if (w[j].clicked) { w[j].clicked=false; break; }
            }
            if (!w[i].visited) w[i].visited=true;

            w[i].clicked=true;
        }
        if (w[i].clicked)
            w[i].tex=CLICKED;
        else {
            if (w[i].visited)
                w[i].tex=VISITED;
            else
                w[i].tex=NOTVISITED;
        }
        w[i].Update(win, gm, ev); //gm - gameobject
    }

w[] is my array of Nodes

9
Graphics / Collision detection question
« on: August 18, 2013, 05:30:11 pm »
Hi,

I'm writing a simple arkanoid game with SFML 2.1. I have a question about collision detection. How to detect which side of the paddle (top, left, right) the ball hit?

10
System / Propes use of Clock and Time for smooth movement
« on: August 12, 2013, 02:54:28 pm »
 Hi!

I started writing a simple game in C++ using SFML 2.1. But I have a problem with smooth movement. I create a new clock, then get elapsed time and convert it to microseconds:

Clock clock;
Time time = clock.getElapsedTime();
float elapsed = time.asMicroseconds();

Next, in game loop I move the paddle:

while (window.isOpen()) {
        Event evt;
        while (window.pollEvent(evt)) {
                pad.move(elapsed);
                clock.restart();
        }

The method move:

void Paddle::move(float elapsed)
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) get().move(speed * elapsed * -1, 0);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) get().move(speed * elapsed, 0);
}

Paddle moves, but it's not a smooth movement I'd like to have here. I've read about Clock and Time classes, but still I don't know how to solve this problem. Can anyone help?

Pages: [1]
anything