Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Draw items from queue/stack  (Read 1358 times)

0 Members and 1 Guest are viewing this topic.

pure841

  • Newbie
  • *
  • Posts: 1
    • View Profile
Draw items from queue/stack
« on: February 28, 2015, 05:41:16 am »
What I am trying to do, is have the ability to draw shapes from outside threads that get stored, drawn in the event loop, cleared, and rinse and repeat. I was thinking of using a stack, but I am getting errors left and right when I try to define my type as sf::Drawable.

Here is my main.cpp code:

// Render thread
void RenderThread()
{
        gRender.EventLoop(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
}

int main()
{
        ...

        HANDLE hRenderThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RenderThread, 0, 0, 0);
        SetThreadPriority(hRenderThread, THREAD_PRIORITY_HIGHEST);

        ...
}

Here is my CRender class:

#pragma once

// Includes
#include <Windows.h>
#include <stdio.h>
#include <stack>
#include <SFML/Graphics.hpp>

// CRender Class
class CRender
{

private:

        //
        // Variables
        //

        sf::RenderWindow m_hWindow;
        //std::stack<sf::Drawable> s;


public:

        //
        // Variables
        //

        //
        // Methods
        //

        // Event loop
        void EventLoop(int width, int height)
        {
                m_hWindow.create(FindWindow(NULL, "Untitled - Notepad"));

                // Make transparent
                HWND hwnd = m_hWindow.getSystemHandle();
                SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);


                // Keep running as long as window is open
                while (m_hWindow.isOpen())
                {
                        // check all the window's events that were triggered since the last iteration of the loop
                        sf::Event event;
                        while (m_hWindow.pollEvent(event))
                        {
                                // "close requested" event: we close the window
                                if (event.type == sf::Event::Closed)
                                        m_hWindow.close();
                        }

                        m_hWindow.clear();

                        //for (int i = 0; !s.empty(); ++i, s.pop())
                        //{
                                // Broken
                                //sf::Drawable toDraw = (sf::Drawable *) s.pop();
                        //}

                        m_hWindow.display();
                }
        }

        void DrawBox(int x, int y, sf::Color color, int size, int thickness)
        {
                // Build the box
                sf::RectangleShape box;

                // Set transparent
                box.setFillColor(sf::Color::Transparent);

                // Set thickness
                box.setOutlineThickness(thickness);

                // Set origin
                sf::Vector2f origin((x - size / 2), (y - size / 2));
                box.setOrigin(origin);

                // Set color
                box.setOutlineColor(color);

                sf::Vector2f bsize((x + size / 2), (y + size / 2));
                box.setSize(bsize);

                //s.push(box);
        }
};

extern CRender gRender;

Few comments:
- Yes, I am testing using Notepad...
- For some reason, transparency isint working. I am trying to make the window completley transparent and only show what is being drawn on top of Notepad.
- I have multiple threads that will call DrawBox() which is why I am trying to queue/store the draw shapes.

I am brand new to SFML so any help would be appreciated.

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Draw items from queue/stack
« Reply #1 on: February 28, 2015, 11:45:30 am »
- For some reason, transparency isint working. I am trying to make the window completley transparent and only show what is being drawn on top of Notepad.
SFML doesn't support transparent windows. If you want that you'll have to write your own custom code.

- I have multiple threads that will call DrawBox() which is why I am trying to queue/store the draw shapes.
I'm certain that your usage of threads is useless and even bad. Your GPU doesn't support multithreading as such drawing from different threads won't give you any kind of advantage, on the contrary you'll most likely add a lot of overhead just by using threads.
Additionally multithreading is an advanced topic. You need to know a lot about shared memory, synchronization, race conditions, etc. and just knowing "a bit" about these topics is not enough.
Thus you'll do yourself a favor in not using multiple threads.

As for the other question, I've no idea what you're trying to do, as such we can't really answer it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/