So, I'm making a game using SFML in C++, but when I tried adding a image a really weird OpenGL(i think) error popped up. Doesn't make any sense at all.
First my console was spammed with random text and symbols, then the application crashed and visual studio told me this :
Exception thrown at 0x618EDBF4 (vcruntime140.dll) in SFML_Game.exe: 0xC0000005: Access violation reading location 0x00C54000.
It sounds like it has something to do with not being able to read my picture file I added, though i don't know whats wring with it.
The location of the image is in the same folder my "SFML_Game.vcxproj" is.
I also have no chance of seeing if "Could not load player image" was printed in console since the spam is too quick.
Edit I can see that the picture failed to load now, here's a picture... :
Console Picture
Here's my current code :
#include<iostream>
#include<SFML/Graphics.hpp>
#include <string>
int main(int argc, char* argv[])
{
// Creates a window
sf::RenderWindow Window(sf::VideoMode(800, 600), "SFML Game Engine");
sf::Texture pTexture;
sf::Sprite playerImage;
if (!pTexture.loadFromFile("Player.png"))
{
std::cout << "Could not load player image" << std::endl;
}
playerImage.setTexture(pTexture);
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event) && Window.hasFocus())
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
}
}
Window.draw(playerImage);
Window.display();
}
}
So, it seems that my code was fine and it was a linker error.
I didn't know the difference between static-dynamic, static and dynamic libraries, so I just put them all in there, after removing some of them it started working.
At least in debugging mode.
Pretty much, if someone else has this problem, look up how to set SFML up on their website instead of a video tutorial. ^^
Related
-I am new to C++ and SFML and want to import a png file.
-It worked a view times, but afterwards I got the
message "Build failed, run last success?" most of the time. Sometimes its still working.
-there is no "real" error, so its hard to figure out, what the problem is
-I read earlier, that switching from Debug to Release-Mode could be a reason, but it didnt helped
its working when I dont use:
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
-> but, ofcourse, the sprite is missing then.
I would be happy to have a solution/reason for this or a topic I missed so far to read/ learn about.
Im happy about advices.
Thanks so far.
Alex
VISUAL STUDIO 2019
x64
SFML-2.5.1
WHOLE CODE:
#include"SFML/Graphics.hpp"
#include<iostream>
#include"main.h"
int main(int argc, char** argv[])
{
sf::RenderWindow window(sf::VideoMode(1200,800), "bimWindow");
sf::RectangleShape rs(sf::Vector2f(1000, 700));
rs.setFillColor(sf::Color::Green);
sf::Event event;
sf::Texture texture;
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100,100);
//sprite.scale(sf::Vector2f(3, 3));
rs.setPosition(80, 80);
// run the program as long as the window is open:
while (window.isOpen())
{
//let window open i guess.
while (window.pollEvent(event)); //stay true as long aas it didnt happen or so
{
// "close requested" event:close the window
if(event.type == sf::Event::Closed)
window.close();
}
//RENDER:
window.clear();
window.draw(rs);
window.draw(sprite);
window.display();
}
return 0;
}
Try making sure that the "assets/player.png" file is inside your solution directory (the folder with the .sln file.) Your code seems to run fine on my end, and that is the only error I can think of. If you can, it would help to have the error message(s) that you are receiving. If the png file is in the right place and you still get errors, I would recommend reinstalling SFML and following a tutorial online to ensure that you get everything set up properly.
I am new to c ++ / SFML and I don't know why my program cannot load a texture, I tried putting the image in my project folder and in the debug folder but it didn't work.
Code:
int main() {
sf::RenderWindow window(VideoMode(600,600),"Window");
sf::Event event;
sf::Texture textura;
textura.loadFromFile("icono.png");
sf::Sprite sprite1(textura);
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type== sf::Event::EventType::Closed) {
window.close();
}
}
window.clear();
window.draw(sprite1);
window.display();
}
}
The error is "Unhandled exception at 0x5F6D3647 (vcruntime140.dll) in TestSFML.exe: 0xC0000005: Access violation while reading location 0x00701000" in the line "textura.loadFromFile("icono.png");"
I found the answer and share it in case it helps. The possible solution is to add the SFML debug libraries in "additional dependencies", the libraries are "sfml-graphics-d.lib" and "sfml-system-d.lib".
I am trying to record some sound with sfml and then play it back. I have previously done this successfully with my old headphones that i believe had a 5.1 sound system. But now when i try to do the same thing with my new headphones (7.1 sound). The code throws this error.
AL lib: (EE) SetChannelMap: Failed to match front-center channel (2)
in channel map.
I have tried restarting visual studio. Restarting my computer. Resetting the cache in visual studio.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window;
window.create(sf::VideoMode(800, 500), "Audio check", sf::Style::Close | sf::Style::Resize);
if (!sf::SoundBufferRecorder::isAvailable())
{
// error: audio capture is not available on this system
std::cout << "Something went wrong" << std::endl;
}
// create the recorder
sf::SoundBufferRecorder recorder;
recorder.start(44100);
//record the audio for 5 sec
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
recorder.stop();
//get the buffer from the recorder and play it back
const sf::SoundBuffer& buffer = recorder.getBuffer();
sf::Sound sound(buffer);
sound.play();
sf::Event event;
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color::Blue);
window.display();
}
return EXIT_SUCCESS;
}
Actually SFML doesn't support more than 2 channels recording. If we check documentation of the method setChannelCount() of the class SoundRecorder it only supports up to 2 (channels).
Edit
From openAL library, which sfml is based on (emphasis mine):
hexagon.ambdec
Specifies a flat-front hexagonal speaker setup for 7.1
Surround output. The front left and right speakers are placed at +30
and -30 degrees, the side speakers are placed at +90 and -90 degrees,
and the back speakers are placed at
+150 and -150 degrees. Although this is for 7.1 output, no front-center speaker is defined for the decoder, meaning that speaker
will be silent for 3D sound (however it may still be used with
AL_SOFT_direct_channels or ALC_EXT_DEDICATED output). A "proper" 7.1
decoder may be provided in the future, but due to the nature of the
speaker configuration will have trade-offs.
It seems the library doesn't count with a actual 7.1 decoder.
I am trying to output an image onto the screen using SFML 2.1, C++, and MS Visual Studio Professional 2013. I am getting an unexpected error when trying to load a file into a texture. It outputs a whole bunch of random characters. I'm sure if its how I configured the SFML library with Visual Studio or a problem with the code. Can anyone solve this problem? Thanks.
Here is a screenshot of what it looks like when I run the program (http://i.stack.imgur.com/uMdLT.png):
This is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
For all configuration properties, they look like this:
Linker -> General (http://i.stack.imgur.com/NZg7P.png):
Linker -> Input (http://i.stack.imgur.com/1tPaB.png):
**Please note that if I did not configured the SFML library as I did, then I would be getting an error from my system saying msvcr110d.dll is missing.
Ok I managed to fix this, here is what you need to do:
Set your SUBSYSTEM to WINDOWS:
Add "sfml-main.lib" to your libraries:
Change your Runtime library to /MD. This is because you're not using the debug versions of the SFML 2.1 libraries (and probably can't in VS2013).
Make sure your "fighter jet.png" image is in the right place. By default Visual Studio uses the Project Directory as the working directory. So put the png in the same folder as your vcxproj file.
Are you sure that the picture is in the execution directory of your program ?
You should avoid spaces in file's name (fighter_jet.png).
If you're not sure about execution directory, try with absolute path (to be sure it's a path problem, and not a picture's problem).
I hope it helps.
I've tried this code on my system (Xcode - OSX), with one of my picture, and it works.
Have you tried with another picture ?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
My goal is to build a Game Boy emulator. In order to do this, I would like to embed an SDL2 surface into a wxWidgets window.
I found this tutorial: http://code.technoplaza.net/wx-sdl/part1/, but my program crashes as soon as I run it. However I suspect this was intended for SDL1.2. Part of the program is shown below.
It seems that if I call SDL_Init() and also attempt to show a wxFrame (which, in this case, is MainWindow), it shows the window for a second and then the program crashes. I commented all other calls to SDL in my program so far, so it seems the problem lies with calling Show() on a wxFrame and initing SDL2 in the same program.
So the question is: can SDL2 and wxWidgets 3 work together? If not, could you guys suggest to me good alternatives a GUI of a Game Boy emulator? Does wxWidgets have its own graphics frame like Qt does (I'd rather avoid Qt)?
Thanks very much!
#include "MainApp.h"
#include "MainWindow.h"
#include <stdexcept>
namespace GBEmu {
static void initSDL() {
//This and SDL_Quit() are the only calls to the SDL library in my code
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
throw std::runtime_error("Fatal Error: Could not init SDL");
}
}
bool MainApp::OnInit()
{
try {
//If I comment out this line, the MainWindow wxFrame shows up fine.
//If I leave both uncommented, the window shows up quickly and then
//crashes.
initSDL();
//If I comment out this line and leave initSDL() uncommented,
//the program will not crash, but just run forever.
(new MainWindow("GBEmu", {50,50}, {640,480}))->Show();
} catch(std::exception &e) {
wxLogMessage(_("Fatal Error: " + std::string(e.what())));
}
return true;
}
int MainApp::OnExit() {
SDL_Quit();
return wxApp::OnExit();
}
}
wxIMPLEMENT_APP(GBEmu::MainApp);
EDIT: Here is more information on how it crashes: It crashes with a Segfault in what seems to be the pthread_mutex_lock disassembly file. This is the output in the console with stack trace:
Starting /home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx...
The program has unexpectedly finished.
/home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx crashed
Stack trace:
Error: signal 11:
/home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx(_ZN5GBEmu7handlerEi+0x1c)[0x414805]
/lib/x86_64-linux-gnu/libc.so.6(+0x36ff0)[0x7fb88e136ff0]
/lib/x86_64-linux-gnu/libpthread.so.0(pthread_mutex_lock+0x30)[0x7fb88c12ffa0]
/usr/lib/x86_64-linux-gnu/libX11.so.6(XrmQGetResource+0x3c)[0x7fb88d1ca15c]
/usr/lib/x86_64-linux-gnu/libX11.so.6(XGetDefault+0xc2)[0x7fb88d1a7a92]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x94dcf)[0x7fb88af8edcf]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x97110)[0x7fb88af91110]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(cairo_surface_get_font_options+0x87)[0x7fb88af63e07]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x2b61f)[0x7fb88af2561f]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x2ef95)[0x7fb88af28f95]
This is a screenshot of where it seems to fail (line 7):
Update: In my MainWindow class, I attach a menu bar to the window. However, it seems when I comment out the setting of the menu bar, the window will show up fine even with initing of SDL. The menu bar will show up fine if I have initSDL() commented out but not the setting of the menu bar. Here is where I set the menu bar:
MainWindow::MainWindow(const wxString &title, const wxPoint &pos, const wxSize &size)
:wxFrame(nullptr, wxIDs::MainWindow, title, pos, size){
wxMenu *fileMenu = new wxMenu;
fileMenu->Append(wxID_EXIT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(fileMenu, "&File");
//commenting this line out will allow the window to showup
//and not crash the program
SetMenuBar(menuBar);
}
You are experiencing an old heisenbug.
The workaround is simple: you have to initialize SDL before wxWidgets (basically, before GTK). To achieve this, you have to change
wxIMPLEMENT_APP(GBEmu::MainApp);
to
wxIMPLEMENT_APP_NO_MAIN(GBEmu::MainApp);
so that wxWidgets doesn't hijack your main().
Then you have to create main() manually. In it, initialize SDL, then call wxEntry():
int main(int argc, char** argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cerr << "Could not initialize SDL.\n";
return 1;
}
return wxEntry(argc, argv);
}
More about the bug:
I have googled around a bit and found that this bug has come up in a few places over the years. There are open reports in many bug trackers that have stack traces very similar to the one you get here (with debug symbols).
The oldest report I could find is from 2005 (!!) from the cairo bug tracker (https://bugs.freedesktop.org/show_bug.cgi?id=4373).
My best guess is that the real hiding place of this bug in either in GTK, cairo, or X. Unfortunately I do not currently have the time to look into it more in depth.