Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SDL_C++ error scope

41 views
Skip to first unread message

Heitber Andrés Montilla Ramirez

unread,
May 13, 2022, 11:10:51 PM5/13/22
to
hie veryone I'm learnig c++ and opengl but I have a problem, I followed every step on the tutorial and after some corrections by migrating sdl1 to sdl2 I have the following error: 'window', was not declared in this scope:
(lines 143 and 184)
if there's someone who knows about sdl and opengl it would be useful to get your help:
here's the code:

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>

#if defined (_WIN32) || defined(_WIN64)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // WIN32

#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif // defined

#define GLUT_DISABLE_ATEXIT_HACK

const GLsizei windowWidth = 500;
const GLsizei windowHeight = 500;

GLfloat cubeRotateX = 45.0f;
GLfloat cubeRotateY = 45.0f;

const Uint8 *keys = NULL;

GLvoid establishProjectionMatrix(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(50.0f, (GLfloat)width / (GLfloat)height, 0.1f, 200.0f);
}

GLvoid initGL(GLsizei width, GLsizei height)
{
establishProjectionMatrix(width, height);

glShadeModel(GL_SMOOTH);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_PERSPECTIVE_CORRECTION_HINT);

}

GLvoid displayFPS(GLvoid)
{
static long lastTime = SDL_GetTicks();
static long loops = 0;
static GLfloat fps = 0.0f;

int newTime = SDL_GetTicks();

if (newTime - lastTime > 100)
{
float newFPS = (float)loops / float(newTime - lastTime) * 1000.0f;

fps = (fps + newFPS) / 2.0f;

char title[80];
printf(title, "OpenGL Demo - %2f", fps);

//SDL_WM_SetCaption(title, NULL);
SDL_Window* window = SDL_CreateWindow("SDL2/OpenGL Demo", 30, 30, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);


lastTime = newTime;

loops = 0;
}

loops++;

}

GLvoid drawScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0, 0, -5.0f);
glRotatef(cubeRotateX, 1, 0, 0);
glRotatef(cubeRotateY, 0, 1, 0);

//draw cube
glBegin(GL_QUADS);
//top face
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);

//bottom face
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);

//front face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);

//back face
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);

//left face
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

//right face
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();

glFlush();

SDL_GL_SwapWindow(window); //error window was not declared in this scope

displayFPS();
}

GLboolean checkKeys(GLvoid)
{
static long lastTime = SDL_GetTicks();

const GLfloat speed = 1.0f;
const long updateTime = 10;

if (keys[SDLK_ESCAPE])
return true;

long newTime = SDL_GetTicks();

if (newTime - lastTime > updateTime)
{
if(keys[SDLK_LEFT])
cubeRotateY -= speed;
if(keys[SDLK_RIGHT])
cubeRotateY += speed;
if(keys[SDLK_UP])
cubeRotateX -= speed;
if(keys[SDLK_DOWN])
cubeRotateX += speed;
}

return false;
}

int main(int argc, char **argv)
{

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Unable to initialize SDL %s", SDL_GetError());
exit(1);
}

if ( window == NULL ) //here throws errors //error window was not declared in this scope

{
fprintf(stderr, "Unable to create OpenGL scene: %s", SDL_GetError());
exit(2);
}

//SDL_Window *screen = SDL_CreateWindow(title, 100, 100, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);

initGL(windowWidth, windowHeight);

SDL_GL_SetSwapInterval(1);


keys = SDL_GetKeyboardState(NULL);


int done = 0;

while ( !done )
{
drawScene();

SDL_Event event;
while( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
done = 1;

keys = SDL_GetKeyboardState(NULL);
}

if ( checkKeys() )
done = 1;
}

SDL_Quit();

return 1;
}



Alf P. Steinbach

unread,
May 14, 2022, 5:49:53 AM5/14/22
to
The code appears to have been jumbled by inadvertent editing, lines
moved out of place; perhaps there are also lines outright missing now.

Go back to source and find correct code.

Cheers,

- Alf

Message has been deleted

Heitber Andrés Montilla Ramirez

unread,
May 14, 2022, 2:41:34 PM5/14/22
to
0 new messages