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

Pages: [1]
1
General / sf::Image::loadFromMemory read access violation
« on: July 18, 2022, 07:41:25 pm »
so what im trying to do is take a screenshot from the entire screen(not only from sfml window) and saving it to memory to then save to a file (i am trying to just save to memory but to debug i am saving) and it gives me the error at the title (i am only using 1 file for a while)

#include "functions.h"
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <Windows.h>

#undef IS_USING_TEXT

void* screenCapturePart() {
        HDC hdcSource = GetDC(NULL);
        HDC hdcMemory = CreateCompatibleDC(hdcSource);

        int capX = GetDeviceCaps(hdcSource, HORZRES);
        int capY = GetDeviceCaps(hdcSource, VERTRES);

        HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, 1920, 1080);
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);

        BitBlt(hdcMemory, 0, 0, 1920, 1080, hdcSource, 0, 0, SRCCOPY);
        hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);

        return hBitmap;
}

int WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
#ifdef IS_USING_TEXT
        static auto defFont = new sf::Font;
        defFont->loadFromFile("Roboto-Regular.ttf");
#endif // IS_USING_TEXT

        sf::RenderWindow window(sf::VideoMode(800u, 800u), "SFML", sf::Style::Close);
        window.setVerticalSyncEnabled(true);
        sf::Event evnt;

        sf::Image screenshot;
        screenshot.loadFromMemory(screenCapturePart(), (size_t)1920 * 1080 * sizeof(sf::Color));

        if (screenshot.saveToFile("z.bmp"))
                window.close();

        while (window.isOpen())
        {
               

                ProcessEvents(window, evnt);
                window.clear();
               
                window.display();
        }
       
#ifdef IS_USING_TEXT
        delete defFont;
#endif // IS_USING_TEXT
        return 0;
}

2
Window / nothing is moving on screen
« on: January 17, 2022, 08:13:22 pm »
I want to make a pong game purely to practice since i am a beginner in programming, but nothing is moving on screen. Can someone help me fix this?

#include "game.h"
#include <SFML/Graphics.hpp>

bool game::isClosing()
{
        while (this->Game.isOpen())
        {
                while (this->Game.pollEvent(this->event))
                {
                        if (this->event.type == sf::Event::Closed)
                        {
                                return true;
                        }
                }
        }
        return false;
}

void game::resetGame()
{
        Ball.circle.setPosition(this->Game.getSize().x / 2.0f, this->Game.getSize().y / 2.0f);
        player1.rectangle.setPosition(player1.rectangle.getPosition().x, 450);
        player2.rectangle.setPosition(player2.rectangle.getPosition());
}

void game::render()
{
        this->Game.clear();

        this->Game.draw(Ball.circle);
        this->Game.draw(player1.rectangle);
        this->Game.draw(player2.rectangle);

        this->Game.display();
}

void game::run()
{
        while (this->Game.isOpen())
        {
                player1.movement();
                player2.movement();
                Ball.movement();
                Ball.bounce(player1);
                Ball.bounce(player2);

                this->render();

                if (this->isClosing())
                {
                        this->Game.close();
                }
        }
}

#include "player.h"
#include <SFML/Graphics.hpp>

void player::movement()
{
        switch (this->side)
        {
        case 'l':
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                        this->rectangle.move(0, -this->speed);
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                        this->rectangle.move(0, this->speed);
                }
                break;

        case 'r':
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        this->rectangle.move(0, -this->speed);
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        this->rectangle.move(0, this->speed);
                }
                break;

        default:
                break;
        }
}

#include "ball.h"
#include <SFML/Graphics.hpp>

void ball::movement()
{
        this->circle.move(this->xSpeed, this->ySpeed);
}

void ball::bounce(const player& player)
{
        if (this->circle.getGlobalBounds().intersects(player.rectangle.getGlobalBounds()))
        {
                if (this->ySpeed < 0)
                {
                        if (this->circle.getPosition().y < player.rectangle.getPosition().y)
                        {
                                this->xSpeed *= -1;
                        }
                        else if (this->circle.getPosition().y > player.rectangle.getPosition().y)
                        {
                                this->xSpeed *= -1;
                                this->ySpeed *= -1;
                        }
                }
                else if (this->ySpeed > 0)
                {
                        if (this->circle.getPosition().y < player.rectangle.getPosition().y)
                        {
                                this->xSpeed *= -1;
                                this->ySpeed *= -1;
                        }
                        else if (this->circle.getPosition().y > player.rectangle.getPosition().y)
                        {
                                this->xSpeed *= -1;
                        }
                }
        }
}

Pages: [1]
anything