I just migrated it an single animation app (just one model) from GLFW+AntTweakBar to SDL2+ImGui .
OpenGL code is the same but i am seem to experiencing more than half of FPS drop
with SDL2+ImGui .
While on GLFW i have an average fps of 100 and on SDL2 i have an average of 30-40.
SDL/GL init code is below :
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, ¤t);
gWindow = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (gWindow == NULL)
{
std::cout << "Window could not be created! SDL Error: " << SDL_GetError() << std::endl;
success = false;
}
else
{
std::cout << std::endl << "Yay! Created window sucessfully!" << std::endl << std::endl;
//Create context
gContext = SDL_GL_CreateContext(gWindow);
if (gContext == NULL)
{
std::cout << "OpenGL context could not be created! SDL Error: " << SDL_GetError() << std::endl;
success = false;
}
else
{
//Initialize GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK)
{
std::cout << "Error initializing GLEW! " << glewGetErrorString(glewError) << std::endl;
}
//Use Vsync
if (SDL_GL_SetSwapInterval(1) < 0)
{
std::cout << "Warning: Unable to set Vsync! SDL Error: " << SDL_GetError << std::endl;
}
If i try setting SDL_GL_SetSwapInterval(0) for immediate update i get an average of 60FPS but it still doesn't look smooth and there is small tearing on the model.
I tried removing the ImGui code and it's a better as it ought to be but still much worse performance .
Is this normal ?
Related
I am just trying to launch a simple SDL2 example. Compiling is ok, but in time of executing, in this code:
bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL init error: " << SDL_GetError() << std::endl;
return false;
}
return true;
}
I get SDL init error: without any output from SDL_GetError(). Can you tell me, what is the problem?
IDE: Eclipse, OS: Ubuntu.
Update:
The shown piece of code contained an minimal reproducible example. But if you insist, then this code gives the same error:
int main(){
if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL init error: " << SDL_GetError() << std::endl;
return false;
}
}
Update 2:
If I add some more code:
int main(){
if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL init error: " << SDL_GetError() << " |" << std::endl;
// return false;
}
SDL_Window* window = SDL_CreateWindow("Hello World", 200, 200, 460, 200, SDL_WINDOW_SHOWN);
if(window == nullptr){
std::cout << "SDL create window error: " << SDL_GetError() << std::endl;
// return false;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(renderer == nullptr){
std::cout << "SDL create renderer error: " << SDL_GetError() << std::endl;
// return false;
}
If I add more code to the project, I get the same error. That is, during initialization on the first check. Further execution occurs without errors, a window and a renderer are created. But the renderer cannot draw anything, the window is always black and no drawing commands are executed:
SDL_SetRenderDrawColor(renderer, 0xFF, 0xAA, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect actor1 = {10, 10, 550, 50};
SDL_RenderFillRect(renderer, &actor1);
Game Engine Creation course work, questions about SDL, currently learning C++ SDL.
Met a question about SDL_Image, Couldn't open image.
When I run the code it closed instantly
bool InitSDL()
{
//Setup SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL did not initialise. Error: " << SDL_GetError();
return false;
}
else
{
gWindow = SDL_CreateWindow(
"GEC_SDL",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (gWindow == NULL)
{
cout << "Window was not created. Error: " << SDL_GetError();
return false;
}
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer != NULL)
{
cout << "Renderer initialised." << endl;
//Initialise PNG loading.
int imageFlags = IMG_INIT_PNG;
if (!(IMG_Init(imageFlags) & imageFlags))
{
cout << "SDL_Image could not initialise. Error: " << IMG_GetError;
return false;
}
else {
cout << "SDL_Image initialised." << endl;
}
}
else
{
cout << "Renderer could not initialise. Error: " << SDL_GetError;
return false;
}
gTexture = LoadTextureFromFile("D:/_a'Programs/School/GEC_SDL/GEC_SDL/Images/text.bmp");
if (gTexture == NULL)
{
return false;
}
return true;
}
Error Messgae
Tried full path: D:_a'Programs\School\GEC_SDL\GEC_SDL\Images
Error Messgae
Load texture from file function
SDL_Texture* LoadTextureFromFile(string path)
{
FreeTexture();
SDL_Texture* pTexture = NULL;
//Load the image.
SDL_Surface* pSurface = IMG_Load( path.c_str() );
if (pSurface != NULL)
{
//Create the texture from the pixels on the surface.
pTexture = SDL_CreateTextureFromSurface(gRenderer, pSurface);
if (pTexture == NULL)
{
cout << "Unable to create texture from surface. Error: " << SDL_GetError() << endl;
}
SDL_FreeSurface(pSurface);
}
else
{
cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
}
return pTexture;
}
I searched similar question in the forum and tried their ways, none of them works, please help.
OMG, yes it's ''s problem, I rename the folder, now it works...
PS: stupid character sets
I'm trying to open a window with SDL2 in visual studio 2015. I've set a .bmp image in my code to display to the screen in a window, but when I run my code the program returns 0 and closes without a window. The .bmp image is in the project folder. How do you display the window?
#include <SDL.h>
#include <iostream>
int main(int argc, char* args[])
{
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
SDL_Surface *imageSurface = nullptr;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
std::cout << "Game Initialization error: " << SDL_GetError() << std::endl;
{
window = SDL_CreateWindow("Contrast Beta 0.0.1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 920, SDL_WINDOW_HIDDEN | SDL_WINDOW_FULLSCREEN);
if (window == NULL)
std::cout << "Window Creation Failed, Error: " << SDL_GetError() << std::endl;
else
{
//Window Created
windowSurface = SDL_GetWindowSurface(window);
imageSurface = SDL_LoadBMP("Background.bmp");
if (imageSurface == NULL)
std::cout << "Error loading background: " << SDL_GetError() << std::endl;
else
{
SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Remove SDL_WINDOW_HIDDEN; that's all.
I'm developing in VS2013 on a Windows 8.1 machine. The window flashes briefly on the screen (despite having a 2 second delay in the code).
Here is all the code:
#include <SDL.h>
#include <iostream>
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 4;
}
//open a window
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return 3;
}
//renderer
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return 2;
}
//load bitmap
SDL_Surface *bmp = SDL_LoadBMP("helloworld.jpg");
if (bmp == nullptr){
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
return 6;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr){
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
return 5;
}
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
return 0;
}
It exits with a code 6. I was thinking it couldn't find the helloworld.jpg file. But, its there. I moved it from location to location hoping I was just an idiot. No luck. Its currently in the same directory as the .exe file.
SDL_LoadBMP will only load BMP formatted pictures, and guessing by the extension you have chosen you look to be using a JPEG formatted image, and your program is refusing to load it.
see SDL_Image for one possible solution.
My current code is as follows:
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
int main(int argc, char **argv) {
if (SDL_Init(SDL_INIT_EVERYTHING != 0)) {
std::cout << "SDL_Init() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Window *win = SDL_CreateWindow("RandomSDL2", 100, 100, 1280, 720, SDL_WINDOW_SHOWN);
if (win = nullptr) {
std::cout << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr) {
std::cout << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Surface *img = IMG_Load("res/test.png");
if (img == nullptr) {
std::cout << "IMG_Load() Error: " << IMG_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, img);
SDL_FreeSurface(img);
if (tex == nullptr) {
std::cout << "SDL_CreateTextureFromSurface() Error: " << SDL_GetError << std::endl;
std::cin.get();
}
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
It seems that the CreateRenderer function throws the error Invalid Window, I'm unsure as to why, I'm currently following this tutorial, I've copied the code down to a T besides the resolution and the use of SDL_image, SDL_image shouldn't really effect the creation of the renderer at this stage, I've also tried the resolution specified in the tutorial and it seems to still throw the same error.
Does anyone have any insight on this issue?
if (win = nullptr) {
^ assignment, probably wanted ==
Don't set win to nullptr and it probably won't be nullptr.