Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:59:09+00:00 2026-05-30T16:59:09+00:00

Last week I bought an old OpenGL book (OpenGL superbible designed for Windows 95)

  • 0

Last week I bought an old OpenGL book (OpenGL superbible designed for Windows 95) for a dollar thinking I could get some of the ideas of drawing in 3D and OpenGL. All of the code uses this weird windows library so I’ve been porting the examples to SDL OpenGL. I’m stuck on a resizable windowed example where a square is drawn in the center no matter what size the window is. For some reason the square only shows up on the first resize and after that I don’t see it again no matter how I resize the window.

Does anybody know what the problem is?

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_opengl.h"

#define TRUE 1
#define FALSE 0

#define WIN_WIDTH 500
#define WIN_HEIGHT 400
#define BPP 32

//go through and get the values to see if everything was set
int check_gl_init(int r_size, int g_size, int b_size, int dbuff) {
    int red, green, blue, doublebuf;

    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    if(red != r_size) { return FALSE; }
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    if(green != g_size) { return FALSE; }
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    if(blue != b_size) { return FALSE; }
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    if(dbuff != doublebuf) { return FALSE; }

    return TRUE;
}

int main(int argc, char** argv) {
    SDL_Init(SDL_INIT_EVERYTHING);
    atexit(SDL_Quit);

    SDL_Surface* screen;
    SDL_Event e;
    int w = WIN_WIDTH; int h = WIN_HEIGHT;
    Uint32 vid_flags = SDL_OPENGL | SDL_RESIZABLE;

    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
        printf("couldn't set double buffering: %s\n", SDL_GetError());
    } else { vid_flags |= SDL_DOUBLEBUF; }

    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    if(info->hw_available == TRUE) { vid_flags |= SDL_HWSURFACE; } else { vid_flags |= SDL_SWSURFACE; }
    printf("hardware surfaces available?: %s\n", (info->hw_available == TRUE ? "yes" : "no"));
    if(info->blit_hw == TRUE) { vid_flags |= SDL_HWACCEL; }
    printf("hardware blits available?: %s\n", (info->blit_hw == TRUE ? "yes" : "no"));

    if(SDL_VideoModeOK(WIN_WIDTH, WIN_HEIGHT, BPP, vid_flags) == 0) {
        printf("error: video mode not supported: `%s'\n", SDL_GetError());
        return 0;
    }
    else {
        screen = SDL_SetVideoMode(WIN_WIDTH, WIN_HEIGHT, BPP, vid_flags);
        if(screen == NULL) {
            printf("no video: `%s'\n", SDL_GetError());
            return 0;
        }
    }

    if(check_gl_init(BPP / 4, BPP / 4, BPP / 4, TRUE) == FALSE) {
        printf("problem setting up opengl: %s\n", glGetString(glGetError()));
        return 0;
    }

    int running = TRUE;
    for(;running;) {
        //process events
        while(SDL_PollEvent(&e)) {
            if(e.type == SDL_VIDEORESIZE) {
                w = e.resize.w; h = e.resize.h;
                if(h == 0) { h = 1; } //prevent division by zero
                screen = SDL_SetVideoMode(w, h, BPP, vid_flags);
                if(screen == NULL) {
                    printf("sdl error, screen died: `%s'\n", SDL_GetError());
                }

                glViewport(0, 0, w, h); //x, y, w, h
                glLoadIdentity(); //reset coordinate system
                glMatrixMode(GL_PROJECTION);

                if(w <= h) {
                    glOrtho(0.0f, 250.0f, 0.0f, 250.0f * (h / w), 1.0, -1.0);
                }
                else {
                    glOrtho(0.0f, 250.0f * (w / h), 0.0f, 250.0f, 1.0, -1.0);
                }

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();

                if(glGetError() != GL_NO_ERROR) {
                    printf("opengl error: %s\n", glGetString(glGetError()));
                }
            }
            if(e.type == SDL_QUIT) { running = FALSE; }
            if(e.type == SDL_KEYDOWN) { if(e.key.keysym.sym == SDLK_q) { running = FALSE; } }
        }

        glClearColor(0.0f, 0.0f, 1.0f, 1.0f); //clear with blue
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glColor3f(1.0f, 0.0f, 0.0f); //red
        glRectf(100.0f, 150.0f, 150.0f, 100.0f);

        glFlush();
        SDL_GL_SwapBuffers();
        SDL_Delay(100);
    }

    SDL_Quit();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:59:10+00:00Added an answer on May 30, 2026 at 4:59 pm

    You have this:

    glLoadIdentity(); //reset coordinate system
    glMatrixMode(GL_PROJECTION);
    

    That sequence will load the identity matrix on to the GL_MODELVIEW stack, since that was the most recent stack set via glMatrixMode() during the last resize.

    glOrtho() then goes on to multiply a new ortho projection matrix by the last one. As you saw this doesn’t work very well.

    Set your matrix mode, then load the identity matrix:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //reset coordinate system
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Something really strange is going on here. I bought a new Mac last week,
I just started developing my brand new windows 8 application last week using mvvm
Last week, I get very depressed, I have three big apps in production and
Just last week, I was doing some PHP stuff. I worked a little solution
During last week I received some code and was asked to improve the performance.
My database experienced some corruption last week, and the technicians from the web hosting
I asked a similar question last week but did not get an answer that
Last week I handled some object casting (DataGridView Columns Control casts) and I tried
I asked a similar question last week and didn't get a very good response,
I just started doing jQuery last week, and so far I already made some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.