Advertisement

Trying to make my first boiler plate opengl sdl 2.0 code

Started by July 13, 2020 06:01 PM
5 comments, last by RedBull4 4ย years, 3ย months ago

Hi I am trying to organise my first boiler plate code for Opengl and SDL2.0 window on linux in C++ or C I plan to change he code to keep the window open and constantly re-draw the sceen to make animations using a true while loop and set it to break the loop with EOF, for the time being why is the screen not appearing black and just appearing transparent, is my opengl code in the right place?or am I missing some code to blank the screen? thanks.

/*
 * -lSDL2
 * -lGL -lGLU -lglut
 */
 
#include <SDL2/SDL.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

int main(int argc, char* argv[])
{
	SDL_Window *window;
	
	SDL_Init(SDL_INIT_VIDEO);
	
	//Create an application window with the following settings
	
	window = SDL_CreateWindow(
	"An SDL2 Window",
	SDL_WINDOWPOS_UNDEFINED,
	SDL_WINDOWPOS_UNDEFINED,
	640,
	480,
	SDL_WINDOW_OPENGL
	);
	
	if (window == NULL) {
		printf("Could not create window: %s\n",SDL_GetError());
		return 1;
	}
	
	glClearColor(0.0,0.0,0.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	SDL_Delay(3000);
	
	SDL_DestroyWindow(window);
	
	SDL_Quit();
	
	return 0;
}

Alio said:
glClearColor(0.0,0.0,0.0,0.0);

try changing the last argument to 1.0

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

I don't think you need those awful looking includes.

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

Advertisement

I don't see where you are swaping buffers to actually reflect changes to the screen

Here try this:

struct myData
{
	SDL_Window *window;
	SDL_GLContext context;
};

int sdlInit(myData * data)
{
	SDL_Init(SDL_INIT_VIDEO);	// | SDL_INIT_AUDIO);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);



	// SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);





	SDL_DisplayMode DM;
	if (SDL_GetDesktopDisplayMode(0, &amp;DM) != 0)
	{
		ALOG("get screen size failed setting to 1024x1024");
		SCREEN_WIDTH = 1024;
		SCREEN_HEIGHT = 1024;
	}
	else
	{
		SCREEN_WIDTH = DM.w;
		SCREEN_HEIGHT = DM.h;
	}

	rSCREEN_WIDTH = SCREEN_WIDTH;
	rSCREEN_HEIGHT = SCREEN_HEIGHT;
	rASPECT = float (rSCREEN_WIDTH) / float (rSCREEN_HEIGHT);
	lcASPECT = rASPECT;

	data->window =
		SDL_CreateWindow("Demo", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
						 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	data->context = SDL_GL_CreateContext(data->window);

	return 0;
}


int glInitA()
{
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClearDepthf(1.0f);//glClearDepth(1.0);
}

int draw_frame(myData * data)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//draw anything

	SDL_GL_SwapWindow(data->window);



	return 0;
}

int main()
{

	myData data;

	sdlInit(&amp;data);

	glInitA();


	bool finished = false;

	while (!finished)
	{
		SDL_Event event;

		draw_frame(&amp;data);

	}

	// bye bye
	return 0;
}

Important note that use full opaque color to display whit clear screen that is glClearColor alpha as 1.0

What type of system do you have? And how do you compile your code?

None

if you have Linux:

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

typedef const char *String;

SDL_Window *window;
SDL_Event event;
SDL_GLContext glContext;

struct WindowData{
    int WIDTH=800, HEIGHT=800;
    int POS_X=SDL_WINDOWPOS_CENTERED, POS_Y=SDL_WINDOWPOS_CENTERED;
    String TITLE="Window";
    const int FLAG=SDL_WINDOW_OPENGL;
} WindowData;

bool windowIsOpen(){
    return window != NULL ? true : false;
}

void initGL(){
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    glContext=SDL_GL_CreateContext(window);

    SDL_GL_SetSwapInterval(SDL_FALSE);
}

void pollEvent(){
    while(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT:
                window=NULL;
                break;
        }
    }
}

void clear(){
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
}

void display(){
    SDL_GL_SwapWindow(window);
}

int main(){
    window=SDL_CreateWindow(WindowData.TITLE, WindowData.POS_X,
                            WindowData.POS_Y, WindowData.WIDTH,
                            WindowData.HEIGHT, WindowData.FLAG);

    initGL();

    while(windowIsOpen()){
        pollEvent();
        clear();
        display();
    }
}

/*
Compilation Code:
g++ main.cpp -o main -lSDL2 -lGL
*/

None

This topic is closed to new replies.

Advertisement