Just made the jump from SDL1.2 to SDL2, been converting my code but couldn't figure out how to resize the window. Here's the code I have now:
SDL_DestroyWindow(Window);
Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenSizeX, ScreenSizeY, SDL_WINDOW_SHOWN);
screen = SDL_GetWindowSurface(Window);
Which as you can see just destroys the window and creates a new one. Sloppy but it works. What I want is to just resize the window, is it possible?
I believe that that you could use the SDL_WINDOW_RESIZABLE flag in SDL_CreateWindow to make the window resizable.
You may look at the wiki doc: SDL_SetWindowSize
To resize a window in SDL, first set it with the flag SDL_WINDOW_RESIZABLE, then detect the resizing window event in a switch and finally call the following methods SDL_SetWindowSize(m_window, windowWidth, windowHeight) and glViewport(0, 0, windowWidth, windowHeight).
In the switch, use the flag SDL_WINDOWEVENT_RESIZED if you want only the final size of the window or SDL_WINDOWEVENT_SIZE_CHANGED if you want all the sizes between the first and the final.
To finish, update your own camera with the new window width and height.
m_window = SDL_CreateWindow("INCEPTION",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
m_windowWidth, m_windowHeight,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
switch (m_event.type) {
case SDL_WINDOWEVENT:
if (m_event.window.event == SDL_WINDOWEVENT_RESIZED) {
logFileStderr("MESSAGE:Resizing window...\n");
resizeWindow(m_event.window.data1, m_event.window.data2);
}
break;
default:
break;
}
void InceptionServices::resizeWindow(int windowWidth, int windowHeight) {
logFileStderr("MESSAGE: Window width, height ... %d, %d\n", windowWidth, windowHeight);
m_camera->resizeWindow(windowWidth, windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
}
Window = SDL_CreateWindow(
"Test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
ScreenSizeX,
ScreenSizeY,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);
Use this function call
Related
I would like help with building a layered rendering system in SDL2.
I have a first layer containing a map of Paris with its roads.
I need to draw a line between two points on this map - the problem occurs when the before state of this line does not disappear.
I need to draw this line over the map and keep this.
How do I make s a system to save the map state whithout any overlaid lines drawn, and such that frame by frame I can show the map with the new state of the line overlaid on top of this?
Solved, below an example !! Thank you all
SDL_Window *window;
SDL_Renderer *render;
SDL_Texture *map; //map texture (my layer)
window = SDL_CreateWindow("Test window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
map = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480); //Creating a texture
/*Map is a red background stored in map texture*/
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_SetRenderTarget(render, map);
SDL_RenderClear(render);
SDL_SetRenderTarget(render, NULL);
/*Seting the line color*/
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
/*Coping the map texture to the render and drawing a green line on top of this*/
SDL_RenderCopy(render, map, NULL, NULL);
SDL_RenderDrawLine(render, 0, 0, 640, 480);
SDL_RenderPresent(render);
SDL_Delay(2000);
/*Another line*/
SDL_RenderCopy(render, map, NULL, NULL);
SDL_RenderDrawLine(render, 0, 480, 640, 0);
SDL_RenderPresent(render);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
Im created GLX window, that contains CWOverrideRedirect attribute, and destroyed input shape, so it looks like OpenGL overlay. Now I am wanted to draw a 2d text or use ImGUI. A lot of examples use GLFW library. So, the main problem is to make GLFWwindow transparency and without input shape.
int attr_mask =
CWOverrideRedirect| //borderless, always on top
CWColormap|
CWBorderPixel|
CWEventMask;
window_handle = XCreateWindow( Xdisplay,
Xroot,
x, y, width, height,
0,
visual->depth,
InputOutput,
visual->visual,
attr_mask, &attr);
..............
// Destroy input shape
XRectangle rect;
XserverRegion region = XFixesCreateRegion(Xdisplay, &rect, 1);
XFixesSetWindowShapeRegion(Xdisplay, window_handle, ShapeInput, 0, 0, region);
XFixesDestroyRegion(Xdisplay, region);
if ((del_atom = XInternAtom(Xdisplay, "WM_DELETE_WINDOW", 0)) != None) {
XSetWMProtocols(Xdisplay, window_handle, &del_atom, 1);
}
From ImGUI opengl3 example
GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui OpenGL3 example", NULL, NULL);
glfwMakeContextCurrent(window);
gl3wInit();
Originally in SDL, the following code could be used to setup a surface:
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 8, 0);
Now, in SDL2 the following code must be used:
SDL_Window *window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
SDL_Surface *surface = SDL_GetWindowSurface(window);
My problem is that SDL_CreateWindow offers no way to set the bbp of the window or surface. It seems to default to 32 bit. I've tried using SDL_ConvertSurfaceFormat but surface->format->BitsPerPixel stays at 32.
How can I create a 8 bit surface in SDL 2? I realize I will need a color palette.
Use SDL_CreateRGBSurface where you can specify number of bits.
Example:
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &sdlWindow, &sdlRenderer);
SDL_Surface *screen = SDL_CreateRGBSurface(0, 640, 480, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
SDL_Texture *sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
640, 480);
SDL_UpdateTexture(sdlTexture, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
I can create a SDL2 window but I don't know how to change background color of this window.
My code:
#include "SDL.h"
SDL_Window *window;
void main()
{
window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Delay(3000);
}
How can I change background color of this window to black?
You should set drawing colour with SDL_SetRenderDrawColor and then use SDL_RenderClear:
(code comes directly from SDL wiki)
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Renderer* renderer;
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
0);
// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, 0);
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
// Give us time to see the window.
SDL_Delay(5000);
// Always be sure to clean up
SDL_Quit();
return 0;
}
If you do not use SDL2 renderer you can do this:
Just call:
SDL_GL_SwapWindow(window);
After :
SDL_GL_MakeCurrent(window, context);
That's all! Now you have a black screen.
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(){
int width=512, height=512;
SDL_Window *window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
if(window==NULL)
cout << "There was an error while initializing the window" << endl << SDL_GetError << endl;
SDL_Event event;
bool running=true;
while(running){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
running=false;
break;
}
}
SDL_GetWindowSize(window, &width, &height);
SDL_Surface *surface=SDL_GetWindowSurface(window);
Uint32 skyblue=SDL_MapRGB(surface->format, 65,193,193);
SDL_FillRect(surface, NULL, skyblue);
SDL_UpdateWindowSurface(window);
}
SDL_DestroyWindow(window);
SDL_Quit();
}
Note, none of these solutions work properly with OpenGL. The question was how to init the window with predefined color, not how to paint the first frame. There's still a blink of white color before the first frame is painted.
Try this one :
SDL_Window *wind;
SDL_Surface *windSurface = NULL
void main()
{
wind = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windSurface = SDL_GetWindowSurface(wind);
SDL_FillRect(windSurface, NULL, SDL_MapRGB(windSurface->format, 240, 200, 112));
SDL_UpdateWindowSurface(wind);
SDL_Delay(3000);
}
I am using SDL2 in windowed mode on Windows 7 64bit to create a small game.
I get tearing at the same height of the window despite having VSYNC active. The weird thing is that the movement in itself is fluid, but there is this line, about 50px from the bottom, where tearing is noticeable. It always stays there, without moving or anything.
The weird thing is that if I crate a smaller window, I get tearing at exactly the same height from the bottom.
I tried taking screens of it but it doesn't show the tearing.
I have no idea if this is a problem of SDL2, a problem with Windows, a problem with my pc or a problem in my source file. I have tried looking online for a solution but I could not find anything like this problem.
It is a first for me since I have been playing with a lot of SDL games in the past in windowed mode and none had this problem.
Here is a snippet of the source I have been using:
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
const int SCREEN_WIDTH = 400;
const int SCREEN_HEIGHT = 300;
bool init( SDL_Window** window, SDL_Renderer** renderer ) {
SDL_Init(SDL_INIT_VIDEO);
*window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
*renderer = SDL_CreateRenderer(*window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
int flags = IMG_INIT_PNG;
IMG_Init(flags);
return true;
}
void close( SDL_Texture** image, SDL_Window** window ) {
SDL_DestroyTexture( *image );
*image = NULL;
SDL_DestroyWindow( *window );
*window = NULL;
SDL_Quit();
}
int main(int argc, char* argv[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* image = NULL;
const SDL_Rect rect = { 0, 0, SCREEN_WIDTH, 200 };
SDL_Rect rectImage, rectImageOriginal;
int w, h;
SDL_QueryTexture(image, NULL, NULL, &w, &h);
rectImage = { 0, 0, w, h };
rectImageOriginal = rectImage;
init( window, renderer );
image = IMG_LoadTexture( renderer, path );
while (running) {
[... here I move rectImage ...]
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
SDL_RenderFillRect(renderer, NULL);
SDL_RenderCopy(renderer, image, &rectImageOriginal, &rectImage);
SDL_RenderPresent(renderer);
}
close(&image, &window);
}