SFML Bullet not spawning - c++

So I am using SFML and c++ to create a simple space game. For the life of can't get a bullet to spawn. This is my source... I am just trying to learn how to go about spawning new sprites into a game.
```
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
int main()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture_sprite;
if (!texture_sprite.loadFromFile("cb.bmp"))
return EXIT_FAILURE;
sf::Sprite sprite(texture_sprite);
sf::Texture texture_background;
if (!texture_background.loadFromFile("space.png"))
return EXIT_FAILURE;
sf::Sprite background(texture_background);
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
return EXIT_FAILURE;
sf::Sprite fire_sprite(texture_fire);
fire_sprite.setScale(4.0f, 1.6f);
std::string direction = "";
bool fire = false;
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
app.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sprite.getPosition().x > -20)
{
sprite.move(-10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Left";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sprite.getPosition().x < 670 )
{
sprite.move(10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Right";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sprite.getPosition().y > 0)
{
sprite.move(0,-10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Up";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sprite.getPosition().y < 480 )
{
sprite.move(0,10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Down";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
fire = true;
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
{
fire = false;
}
if(fire == true)
{
std::cout << "FIRE!!!" << std::endl;
fire_sprite.setPosition((sprite.getPosition()));
std::cout << "Fire positions is " <<fire_sprite.getPosition().x << " " << fire_sprite.getPosition().y << "The direction is " << direction <<std::endl;
}
}
app.clear();
// Draw the sprite
app.draw(background);
app.draw(sprite);
app.draw(fire_sprite);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
```

sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
Instead of loading texture to texture_fire you load again to texture_background. It should be:
if (!texture_fire.loadFromFile("fire_1.png"))

Related

Error with bullets spawn in SFML

I have a little problem with spawning bullets in my SFML game.
There's code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
int pad_id;
int pociski = 3;
int d_pad_x, d_pad_y;
int klatki;
bool p_1;
bool p_2;
bool p_3;
sf::Texture tekstura, pocisk;
sf::Sprite kulka, pocisk_1, pocisk_2, pocisk_3;
sf::Clock _clock;
float accumulator = 0;
float TIME_STEP = 0.03f;
sf::ContextSettings settings;
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Wrecking balls", sf::Style::Fullscreen, settings);
class bullet
{
public:
sf::Texture bullet_texture;
sf::Sprite _sprite;
void spawn_new_bullet(sf::Vector2f from, sf::Vector2f to);
};
void bullet::spawn_new_bullet(sf::Vector2f from, sf::Vector2f to)
{
bullet_texture.loadFromFile("pocisk.png");
_sprite.setTexture(bullet_texture);
_sprite.setPosition(from);
_sprite.setRotation(atan2(to.y - from.y, to.x - from.x));
window.draw(_sprite);
}
int main()
{
settings.antialiasingLevel = 2;
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
if(!tekstura.loadFromFile("ball.png"))
{
std::cout << "error" << std::endl;
}
if (!pocisk.loadFromFile("pocisk.png"))
{
std::cout << "error" << std::endl;
}
kulka.setTexture(tekstura);
pocisk_1.setTexture(pocisk);
pocisk_2.setTexture(pocisk);
pocisk_3.setTexture(pocisk);
for (int i=0; i<8; i++)
{
if (sf::Joystick::isConnected(i))
{
std::cout << "Pad " << i << " is connected." << std::endl << "Number of buttons: " << sf::Joystick::getButtonCount(i) << std::endl;
pad_id = i;
}
}
kulka.setScale(0.050, 0.050);
pocisk_1.setScale(0.050, 0.050);
pocisk_2.setScale(0.050, 0.050);
pocisk_3.setScale(0.050, 0.050);
while (window.isOpen())
{
accumulator += _clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::JoystickButtonPressed)
{
if (sf::Joystick::isButtonPressed(0, 0))
{
std::cout << "0" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 1))
{
std::cout << "1" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 2))
{
std::cout << "2" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 3))
{
std::cout << "3" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 4))
{
std::cout << "4" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 5))
{
std::cout << "5" << std::endl;
if (pociski > 0)
{
if (pociski == 3)
{
p_3 = true;
}
if (pociski == 2)
{
p_2 = true;
}
if (pociski == 1)
{
p_1 = true;
}
pociski -= 1;
}
}
if (sf::Joystick::isButtonPressed(0, 6))
{
std::cout << "6" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 7))
{
std::cout << "7" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 8))
{
std::cout << "8" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 9))
{
std::cout << "9" << std::endl;
}
}
}
if(accumulator > TIME_STEP)
{
window.clear();
if (pociski < 3)
{
klatki += 1;
}
d_pad_x = sf::Joystick::getAxisPosition(pad_id, sf::Joystick::X);
d_pad_y = sf::Joystick::getAxisPosition(pad_id, sf::Joystick::Y);
kulka.move(d_pad_x / 5, d_pad_y / 5);
pocisk_1.setPosition(kulka.getPosition().x + 10, kulka.getPosition().y + 13.75);
pocisk_2.setPosition(kulka.getPosition().x + 20, kulka.getPosition().y + 13.75);
pocisk_3.setPosition(kulka.getPosition().x + 30, kulka.getPosition().y + 13.75);
//std::cout << d_pad_x << d_pad_y << std::endl;
window.draw(kulka);
if (pociski == 1)
{
window.draw(pocisk_1);
}
else if (pociski == 2)
{
window.draw(pocisk_1);
window.draw(pocisk_2);
}
else if (pociski == 3)
{
window.draw(pocisk_1);
window.draw(pocisk_2);
window.draw(pocisk_3);
}
if (p_3 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_3 = false;
}
if (p_2 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_2 = false;
}
if (p_1 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_1 = false;
}
accumulator -= TIME_STEP;
window.display();
}
if (klatki >= 166)
{
if (pociski < 3)
{
pociski += 1;
klatki = 0;
}
}
}
return 0;
}
The error I got in line 162 is: "missing template arguments before '(' token". Can anyone tell me what I should do? And why this method has to be used with a template?
Vector2 is a template type. Either provide the template parameters, or use one of the provided typedefs such as sf::Vector2i – Neil Kirkckquote
Give correct second argument to your function spawn_new_bullet, for example Vector2f().
Cheer!

Code::Blocks executable file doesn't have textures

I've made a basic program using the SFML library in Code::Blocks, and now all I want to do is send it to a friend. I've built my program in both debug and release mode, yet whenever I run the .exe file from my bin, it simply will not load the textures. Of course when I run it within the Code::Blocks client, it works perfectly. I'm not sure if I need to package my work or send the files separately or what, but any help would be much appreciated
Here's the code in case it's helpful somehow (I know it's not pretty):
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <Windows.h>
int main(){
sf::RenderWindow Window;
Window.create(sf::VideoMode(800, 600), "Enjoy :)");
sf::Clock clock;
int pixelFindX = 0, pixelFindY = 0;
int oldLoc = 0, newLoc = 0;
int changeVal = 1;
int tripleCheck = 0;
int xLoc = 0, yLoc = 100;
double timeChecker = 0;
bool execute = false, secondExecute = false;
bool oldSide = false, newSide = false, changer = true;
bool drawLeft = false, drawRight = false, drawCenter = false;
sf::Texture pTexture;
sf::Sprite playerImage;
if(!pTexture.loadFromFile("Pixel Dude.png")){
std::cout << "Could not load pTexture file" << std::endl;
}
sf::Texture bTexture;
sf:: Sprite backgroundImage;
if(!bTexture.loadFromFile("molester moon background IS DONE.jpg")){
std::cout << "Could not load bTexture file" << std::endl;
}
sf::Texture bridgeTexture;
sf::Sprite bridgeImageL;
sf::Sprite bridgeImageR;
if(!bridgeTexture.loadFromFile("pixel bridge.jpg")){
std::cout << "Could not load bridgeTexture file" << std::endl;
}
sf::Texture trophyTexture;
sf::Sprite trophyImage;
if(trophyTexture.loadFromFile("trophy pixeled.png")){
std::cout << "Could not load trophyTexture file" << std::endl;
}
sf::String stringL = "Move to the Left!", stringR = "Move to the Right!", stringC = "You are currently balanced.";
sf::String stringGo = stringL;
sf::Font font;
if(!font.loadFromFile("arial.ttf")){
std::cout << "Could not load font file" << std::endl;
}
sf::Text text(stringGo, font, 50);
text.setPosition(xLoc, yLoc);
text.setStyle(sf::Text::Bold);
playerImage.setTexture(pTexture);
playerImage.setPosition(336, 355);
playerImage.setScale(4,4);
playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));
backgroundImage.setTexture(bTexture);
backgroundImage.setPosition(0,0);
bridgeImageL.setTexture(bridgeTexture);
bridgeImageL.setPosition(0, 450);
bridgeImageL.setScale(4.17,4.68);
bridgeImageR.setTexture(bridgeTexture);
bridgeImageR.setPosition(400,450);
bridgeImageR.setScale(4.17, 4.68);
trophyImage.setTexture(trophyTexture);
trophyImage.setPosition(600, 305);
trophyImage.setScale(2,2);
sf::View view;
sf::View viewReg;
view.reset(sf::FloatRect(0, 0, 800, 600));
view.setViewport(sf::FloatRect(0, 0, 1, 1));
viewReg.reset(sf::FloatRect(0, 0, 800, 600));
viewReg.setViewport(sf::FloatRect(0, 0, 1, 1));
std::cout << "Player Dimensioms: Width = " << playerImage.getGlobalBounds().width << ", Height = " << playerImage.getGlobalBounds().height << std::endl;
while(Window.isOpen()){
clock.restart();
sf::Event Event;
while(Window.pollEvent(Event)){
switch(Event.type){
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if(Event.key.code == sf::Keyboard::Escape){
Window.close();
}
break;
}
}
//std::cout << timeChecker << std::endl;
timeChecker += clock.getElapsedTime().asSeconds();
if(timeChecker >= .00025){
execute = true;
timeChecker = 0;
//std::cout << "execute = true" << std::endl;
}
oldLoc = playerImage.getPosition().x;
oldSide = changer;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && execute){
playerImage.move(1, 0);
pixelFindY = 2;
tripleCheck++;
changer = true;
//std::cout << "d called" << std::endl;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && execute){
playerImage.move(-1, 0);
pixelFindY = 1;
tripleCheck++;
changer = false;
//std::cout << "a called" << std::endl;
}
if(playerImage.getPosition().x < -96){
playerImage.setPosition(800, playerImage.getPosition().y);
} else if(playerImage.getPosition().x > 800){
playerImage.setPosition(-96, playerImage.getPosition().y);
}
newLoc = playerImage.getPosition().x;
newSide = changer;
pixelFindX += changeVal;
if(pixelFindX > 2){
pixelFindX -= 2;
changeVal = -1;
} else if(pixelFindX < 0){
pixelFindX += 2;
changeVal = 1;
}
if(oldLoc != newLoc && tripleCheck > 40 || oldSide != newSide){
playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));
tripleCheck = 0;
//std::cout << "Yes called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
} else {
//std::cout << "-----Not called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
}
execute = false;
/* if(playerImage.getPosition().x + 128 > trophyImage.getPosition().x && playerImage.getPosition().x < trophyImage.getPosition().x + 192){
view.rotate(.0001);
}
*/
if(playerImage.getPosition().x > 500 - 128){
view.rotate(-.01);
drawRight = true;
} else if (playerImage.getPosition().x < 300 - 128){
view.rotate(.01);
drawLeft = true;
} else{
drawCenter = true;
}
//std::cout << "Player X: " << playerImage.getPosition().x + 128 << ", Trophy X: " << trophyImage.getPosition().x << std::endl;
if(drawLeft){
stringGo = stringR;
xLoc = 0;
} else if (drawRight){
stringGo = stringL;
xLoc = 800 - text.getGlobalBounds().width - 10;
} else if(drawCenter){
stringGo = stringC;
xLoc = 400 - text.getGlobalBounds().width / 2;
}
text.setPosition(xLoc, yLoc);
text.setString(stringGo);
Window.setView(viewReg);
Window.draw(backgroundImage);
Window.draw(text);
Window.setView(view);
Window.draw(bridgeImageL);
Window.draw(bridgeImageR);
// Window.draw(trophyImage);
Window.draw(playerImage);
Window.display();
Window.clear();
drawLeft = false;
drawRight = false;
drawCenter = false;
//Sleep(50);
}
}
Copy the compiled executable into a new folder and paste there the textures too. This folder you can send your friend (e.g. in a rar-archive).The textures won't load because probably the textures weren't in the bin/debug or bin/release folder. There are in another place wich is in the debugging mode is set as the working directory.If you want to change that (what I would not recommend) you can go to Project -> Properties -> Build targets -> [name of target] -> Execution working dir and change it to the directory where the compiled executables went.

Check mouse wheel state SFML

In my program I wish to be able to know if the mouse wheel has been scrolled, and if so how much in what direction. Is this possible with C++ and SFML?
So far I have this:
if (sf::Event::MouseWheelEvent().delta != 0)
{
SimulationView.zoom(1 + (10 / sf::Event::MouseWheelEvent().delta));
}
But the second line never exicutes, even when I scroll the mouse wheel
You can read the mouse wheel as part of the event loop that is polled once per frame:
int main()
{
sf::RenderWindow window(sf::VideoMode(320, 256), "Title");
sf::Event event;
while(window.isOpen())
{
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
else if(event.type == sf::Event::MouseWheelMoved)
{
// display number of ticks mouse wheel has moved
std::cout << event.mouseWheel.delta << '\n';
}
}
window.clear();
// draw window here
window.display();
}
}
Note: For SFML 2.3
Thanks to #Hiura for pointing out that sf::Event::MouseWheelMoved is deprecated in SFML 2.3.
Use this instead:
if(event.type == sf::Event::MouseWheelScrolled)
{
if(event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
std::cout << "wheel type: vertical" << std::endl;
else if(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel)
std::cout << "wheel type: horizontal" << std::endl;
else
std::cout << "wheel type: unknown" << std::endl;
std::cout << "wheel movement: " << event.mouseWheelScroll.delta << std::endl;
std::cout << "mouse x: " << event.mouseWheelScroll.x << std::endl;
std::cout << "mouse y: " << event.mouseWheelScroll.y << std::endl;
}

SFML Networking problems

I'm a noob to SFML networking, i think i'm doing it all wrong, i'm constantly getting errors with this code. All i want to do is send packets of the position then update them on the other window, here is what i have so far.
#include <iostream>
#include <string>
#include "player.h"
#include "gameAssets.h"
#include "zombie.h"
using namespace std;
sf::Vector2u size(1000, 800);
bool focused = true;
sf::Vector2f newPosition, oldPosition;
//Multiplayer code.
//sf::Thread* thread = 0;
char choice;
sf::Mutex globalMutex;
sf::TcpSocket socket;
sf::IpAddress ip;
void sendandReceiveData(){
//Server half
sf::Packet packetSendX, packetSendY;
packetSendX << oldPosition.x;
packetSendY << oldPosition.y;
socket.send(packetSendX);
socket.send(packetSendY);
sf::Packet packetReceiveX, packetReceiveY;
//Client half
socket.receive(packetReceiveX);
socket.receive(packetReceiveY);
packetReceiveX >> newPosition.x;
packetReceiveY >> newPosition.y;
cout << "Other Players X: " << newPosition.x << endl;
cout << "Other Players Y: " << newPosition.y << endl;
}
int main(){
sf::RenderWindow window(sf::VideoMode(size.x, size.y), "Zombie Defence", sf::Style::Titlebar | sf::Style::Close);
//Limited fps so we dont need deltaTime...
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
player player;
zombie zombie;
gameAssets gameAssets;
player.initialize();
zombie.initialize();
gameAssets.initialize();
gameAssets.loadContent();
zombie.loadContent();
player.loadContent();
//Multiplayer code.
system("CLS");
cout << "Multiplayer Dev Build 0.1" << endl;
cout << " " << endl;
cout << "Enter (S) for server or (C) for client." << endl;
cin >> choice;
if (choice == 'S'){
sf::TcpListener listener;
listener.setBlocking(false);
listener.listen(5000);
listener.accept(socket);
cout << "New Client Connected: " << socket.getRemoteAddress() << endl;
}
else if (choice == 'C'){
cin >> ip;
if (socket.connect(ip, 5000) == sf::Socket::Done){
cout << "Connected to server" << endl;
}
}
while (window.isOpen()){
sf::View view = player.getView();
sf::Vector2f playerPosition = player.getPosition();
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed){
window.close();
}
if (event.type == sf::Event::GainedFocus){
focused = 1;
}
else if (event.type == sf::Event::LostFocus){
focused = 0;
}
}
//Only update if window is focused...
if (focused){
//Updating
player.update(window);
zombie.followPlayer(playerPosition);
zombie.update(window);
window.setView(view);
}
oldPosition = player.getPosition();
sendandReceiveData();
window.clear();
gameAssets.draw(window);
zombie.draw(window);
player.draw(window);
window.display();
}
return 0;
}
If anyone could give me a hand on what i'm doing wrong it would be appreciated, thanks.
I fixed it. All I had to do was add packetReceiveX.clear(); and the same for the y to fix the problem.

SDL blit surface: Segmentation fault

Every time I run this program I got a this error:
Segmentation fault
testing.cpp
#include <iostream>
#include <cstdint>
#include <SDL.h>
#include <SDL_image.h>
#include "Surface.h"
#include "Point2D.h"
int main() {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cerr << "Can not init SDL" << std::endl;
return -1;
}
SDL_Surface *scr = SDL_SetVideoMode(800, 600, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
if ( scr == NULL ) {
std::cerr << "Can not open window" << std::endl;
return -1;
}
SDL_WM_SetCaption("Test Surface class", NULL);
Surface screen;
Surface image;
screen.set(SDL_GetVideoSurface());
if (image.load("./data/images/logo.png") == false) {
std::cerr << "Can not open file" << std::endl;
return -1;
}
bool run = true;
uint32_t sticks = SDL_GetTicks();
while(run) {
if (screen.draw(image, Rectangle(0, 0, image.getWidth(), image.getHeight()), Point2D(0, 0)) == false) {
std::cerr << "Can not draw to window" << std::endl;
return -1;
}
if ((SDL_GetTicks() - sticks)/1000 > 5) {
std::cerr << "End of time" << std::endl;
run = false;
}
SDL_Flip(screen.sdlSurface());
}
std::cerr << "Done" << std::endl;
SDL_Quit();
return 0;
}
There is the source code of class Surface
#include "Surface.h"
#include "Color.h"
Surface::Surface() {
this->surface = NULL;
}
Surface::~Surface() {
SDL_FreeSurface(this->surface);
this->surface = NULL;
}
bool Surface::draw(Surface source, Rectangle source_area, Point2D position) {
SDL_Rect sr = source_area.sdlRect();
SDL_Rect tr = {position.getX(), position.getY(), 0, 0};
std::cout << sr.x << "," << sr.y << "," << sr.w << "," << sr.h << std::endl;
std::cout << tr.x << "," << tr.y << "," << tr.w << "," << tr.h << std::endl;
std::cout << source.sdlSurface() << std::endl;
std::cout << this->surface << std::endl;
// TR and SR are currently unused. Just for case of testing
if (SDL_BlitSurface(source.sdlSurface(), NULL, this->surface, NULL) != 0) {
return false;
}
return true;
}
bool Surface::fill(Color color, Rectangle area) {
SDL_Rect tr = area.sdlRect();
std::cout << tr.x << "," << tr.y << "," << tr.w << "," << tr.h << std::endl;
std::cout << (int)color.sdlColor() << std::endl;
if ( SDL_FillRect(this->surface, &tr, color.sdlColor()) != 0) {
return false;
}
return true;
}
Rectangle Surface::getSize() {
return Rectangle(0, 0, this->getWidth(), this->getHeight());
}
bool Surface::load(std::string file) {
SDL_Surface *src = IMG_Load(file.c_str());
if (src == NULL) {
std::cout << "cyh" << std::endl;
std::cout << IMG_GetError() << std::endl;
return false;
}
this->surface = SDL_DisplayFormatAlpha(src);
if (this->surface == NULL) {
std::cout << "cyh1" << std::endl;
std::cout << SDL_GetError() << std::endl;
return false;
}
SDL_FreeSurface(src);
src = NULL;
return true;
}
void Surface::set(SDL_Surface *surface) {
this->surface = surface;
}
void Surface::set(Surface surface) {
this->surface = surface.sdlSurface();
}
The problem is probably in the draw function. If I change this line:
if (SDL_BlitSurface(source.sdlSurface(), NULL, this->surface, NULL) != 0) {
to:
if (SDL_BlitSurface(IMG_Load("./data/images/logo.png"), NULL, this->surface, NULL) != 0) {
everything seems to be ok. Could you give me a suggestion where should be the problem?
You're violating The Rule of 3.
You need to define a copy constructor and a copy assignment operator for this to work properly.
When you're passing the Surface in to the draw method, you're making a copy of it. Since you're managing a resource inside the Surface class (the SDL_Surface*), you'll have multiple instances of the class pointing at the same surface, so when the destructors are called, you'd be freeing the same SDL_Surface* more than once.
Edit: I would recommend passing your arguments by const reference. You don't need to make copies of the Surface objects when you want to draw them.
This:
bool Surface::draw(Surface source, Rectangle source_area, Point2D position);
Would become this:
bool Surface::draw(const Surface &source, const Rectangle &source_area, const Point2D &position);
And this can be applied to your other functions too.