Answer the question
In order to leave comments, you need to log in
Access violation. How to solve the problem?
I started writing a game in SFML, but I ran into an error
An exception was thrown at 0x77D9A225 (ntdll.dll) in Project1.exe: 0xC0000005: Access violation while writing at address 0x00000004.
Here is the code itself (there is sfml):
#include<SFML\Graphics.hpp> // SFML графика
#include<SFML\OpenGL.hpp>
#include<SFML\Audio.hpp>
#include<Windows.h> // Виндовс библиотека
sf::RenderWindow window(sf::VideoMode(720, 480) , "My c++ game, rafamont"); // Создаем окно
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { // Скрытие консоли
window.setFramerateLimit(60); // К-ство FPS
image.loadFromFile("image.png");//Загружаем картинку
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setPosition(50, 50);
sprite.setTextureRect(sf::IntRect(0, 0, 64, 96));
// Оставить окно открытым
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(-1, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(1, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(0, -1);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(0, 1);
}
window.clear(sf::Color::Black);
window.draw(sprite);
window.display();
}
}
Answer the question
In order to leave comments, you need to log in
You have a null pointer dereference.
Try moving the creation of the window and other objects to the top of WinMain.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question