The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
"Remove" Rect/image
Tyzor


Joined: 12 Oct 2014
Posts: 10
Hey guys,

Just wondering if there's a function to temporarily "remove" an image from screen?
I'm just trying to make a rectangle appear and not appear once a button is toggled.

Code:

...
switch (e.key.keysym.sym)
{
    case SDLK_a:
        drawSDL_RenderFillRect(...);
        break;
    case SDLK_b:
        **remove the rect**
        break;
}


Any help will be appreciated
Naith


Joined: 03 Jul 2014
Posts: 158
Instead of "removing" the rect / quad, you can choose to only render it when you want to. Use a boolean ('RenderRect ' for example) that you set to True when the mouse pointer is inside it and set it to False when the mouse pointer is outside of it (only an example of when to change the boolean state). Whenever the boolean is True, render the rect / quad, and when it's False, don't render the rect / quad.

Psuedo code:

Code:

bool RenderRect = false;

main()
{
   if(MousePointerInsideRect)
      RenderRect = true;

   else
      RenderRect = false;


   if(RenderRect)
      FunctionToRenderRect();
}

Tyzor


Joined: 12 Oct 2014
Posts: 10
Thanks for the reply. Please bear with me as I've only started SDL Very Happy

I took your advice and used a bool. I was successful in that aspect but the problem now is just refreshing the screen. As of this moment i have 2 rectangles, when i press 1 it'll draw a red rectangle and if i press 1 again it'll clear the screen (this is perfect!). If i press 2, it'll draw a blue rectangle and if i press it again it'll clear the screen. HOWEVER if i have both rectangles on the screen at the same time it'll clear the entire screen resulting in an empty window. Again, any suggestions in terms of design or solutions will be GREATLY appreciated.


Code:

int main(int argc, char* args[])
{

   if (!init())
   {
      std::cout << "Failed to init!" << std::endl;
   }


   else
   {
      bool quit = false;
         
      bool render1 = true;
      bool render2 = true;
      bool render3 = true;

      SDL_Event e;

      SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
      SDL_RenderClear(gRenderer);
      while (!quit)
      {
         while (SDL_PollEvent(&e) != 0)
         {
            if (e.type == SDL_QUIT)
            {
               quit = true;
            }

            if (e.type == SDL_KEYDOWN)
            {
               if (e.key.keysym.sym == SDLK_1)
               {
                  if (render1 == true)
                  {
                     std::cout << "true1 " << std::endl;
                     drawRectangle1();
                     render1 = false;
                  }

                  else if (render1 == false)
                  {
                     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
                     SDL_RenderClear(gRenderer);
                     std::cout << "false1" << std::endl;
                     render1 = true;
                  }
               }

               if (e.key.keysym.sym == SDLK_2)
               {
                  if (render2 == true)
                  {
                     std::cout << "true2 " << std::endl;
                     drawRectangle2();
                     render2 = false;
                  }

                  else if (render2 == false)
                  {
                     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
                     SDL_RenderClear(gRenderer);
                     std::cout << "false2" << std::endl;
                     render2 = true;
                  }
               }
            }
         }
            
         SDL_RenderPresent(gRenderer);
      }
   }
   
   close();

   return 0;
}
Naith


Joined: 03 Jul 2014
Posts: 158
The problem with your code, as of now, is that you're only render the rectangles one frame, which is in the SDL_KEYDOWN event. There's a few things that can be changed in your code. Some of them regarding the design and some of them regarding to make the program works the way it's planned. Here's my version of your code.

Code:

Code:

#include "SDL.h"

#include <iostream>

SDL_Window* m_pWindow = NULL;
SDL_Renderer* m_pRenderer = NULL;

SDL_Event m_Event;

bool m_Running = true;
bool m_Render1 = false;
bool m_Render2 = false;

bool Init()
{   
   // Initialize SDL
   // If there was a problem with initializing SDL, return false
   if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
      return false;

   else
   {
      // Create the SDL window
      m_pWindow = SDL_CreateWindow("Render rectangles", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);

      // If the SDL window has been successfully created
      if(m_pWindow)
      {
         // Create the SDL renderer
         m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);

         // If the SDL renderer has been successfully created
         if(m_pRenderer)
         {
            // Clear the window to white
            SDL_SetRenderDrawColor(m_pRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
         }

         // If there was a problem creating the SDL renderer
         else
            return false;
      }

      // If there was a problem creating the SDL window
      else
         return false;
   }

   // If everything was successfully initialized / created
   return true;
}

void Close()
{
   // Shut down SDL, destroy textures, surfaces and such

   SDL_Quit();
}

void RenderRectangle(int RectangleXPosition, int RectangleYPosition, int RectangleWidth, int RectangleHeight, SDL_Color RectangleColor, bool Filled)
{
   // Create the rectangle to render
   SDL_Rect Rectangle = {RectangleXPosition, RectangleYPosition, RectangleWidth, RectangleHeight};

   // Set the rectangle color
   SDL_SetRenderDrawColor(m_pRenderer, RectangleColor.r, RectangleColor.g, RectangleColor.b, RectangleColor.a);

   // Render a filled rectangle
   if(Filled)
      SDL_RenderFillRect(m_pRenderer, &Rectangle);

   // Render a unfilled rectangle
   else
      SDL_RenderDrawRect(m_pRenderer, &Rectangle);
}

int main(int argc, char* args[])
{
   if(!Init())
      std::cout << "Failed to init!" << std::endl;

   else
   {
      while(m_Running)
      {
         while(SDL_PollEvent(&m_Event))
         {
            if(m_Event.type == SDL_QUIT)
               m_Running = false;

            if(m_Event.type == SDL_KEYDOWN)
            {
               if(m_Event.key.keysym.sym == SDLK_1)
               {
                  /*
                  If the bool is true, it will be changed to false
                  If the bool is false, it will be changed to true
                  */
                  m_Render1 = !m_Render1;

                  // If(m_Render1) is the same as If(m_Render1 == true)
                  if(m_Render1)
                     std::cout << "render1: true " << std::endl;

                  else
                     std::cout << "render1: false" << std::endl;
               }

               if(m_Event.key.keysym.sym == SDLK_2)
               {
                  m_Render2 = !m_Render2;

                  if(m_Render2)
                     std::cout << "render2: true " << std::endl;

                  else
                     std::cout << "render2: false" << std::endl;
               }
            }
         }

         // Clear the current render target
         SDL_RenderClear(m_pRenderer);


         if(m_Render1)
         {
            // The color of the rectangle (red)
            SDL_Color RectangleColor = {0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE};

            // Draw a filled rectangle on the chosen position, in the chosen color
            RenderRectangle(200, 200, 32, 32, RectangleColor, true);
         }

         if(m_Render2)
         {
            // The color of the rectangle (blue)
            SDL_Color RectangleColor = {0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE};

            // Draw a unfilled rectangle on the chosen position, in the chosen color
            RenderRectangle(300, 200, 32, 32, RectangleColor, false);
         }


         // Clear the window to white after the render operations
         SDL_SetRenderDrawColor(m_pRenderer, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);

         // Update the window
         SDL_RenderPresent(m_pRenderer);
      }
   }

   Close();

   return 0;
}

Tyzor


Joined: 12 Oct 2014
Posts: 10
Ahh worked like a charm. Thank you very much.