o.o
o.o

Reputation: 3751

OpenGL and SDL2 cannot open window

I am trying to learn how to use OpenGL and SDL2 but when I run the simple program posted below, the window opens and instantly closes with the following message: exit status 3221225477.

main.go

package main

import (
    "fmt"

    "github.com/go-gl/gl/v4.1-core/gl"
    "github.com/veandco/go-sdl2/sdl"
)

var (
    window  *sdl.Window
    context sdl.GLContext
)

func main() {
    // Initialize sdl and create context
    initialize()

    // Initialize OpenGL pointers
    gl.Init()

    // Setup opengl attributes
    setOpenGLAttributes()

    // This makes our buffer swap syncronized with the monitor's vertical refresh
    sdl.GLSetSwapInterval(1)

    // Run and clean
    run()
    clean()
}

func initialize() {
    sdl.Init(sdl.INIT_EVERYTHING)

    window, err := sdl.CreateWindow("Test", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, 1280, 720, sdl.WINDOW_OPENGL)
    if err != nil {
        fmt.Println(err)
        return
    }

    context, err = window.GLCreateContext()
    if err != nil {
        fmt.Println(err)
        return
    }
}

func setOpenGLAttributes() {
    // SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
    sdl.GLSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE)

    // 3.2 is part of the modern versions of OpenGL, but most video cards whould be able to run it
    sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
    sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2)

    // Turn on double buffering with a 24bit Z buffer.
    sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1)
}

func run() {
    for {
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                return
            }
        }

        gl.ClearColor(1, 8, 20, 1.0)
        gl.Clear(gl.COLOR_BUFFER_BIT)
        window.GLSwap()
    }
}

func clean() {
    // Delete our OpengL context
    sdl.GLDeleteContext(context)

    // Destroy our window
    window.Destroy()

    // Shutdown SDL 2
    sdl.Quit()
}

What am I doing wrong? I was following this tutorial: http://headerphile.com/sdl2/opengl-part-1-sdl-opengl-awesome/ and it doesn't look like I missed anything converting it to Go.

EDIT: So I added gl.Init() before setting the OpenGL attributes. I still get the same behavior, but the program keeps running. The window is invisible (cannot see it in the task bar) and the only way to quit is by pressing CTRL+C in the terminal. Am I missing some other function call?

Upvotes: 3

Views: 508

Answers (1)

VonC
VonC

Reputation: 1328122

You can see a similar crash in ggez/ggez issue 141 (not related to Go, actually in Rust with OpenGL)

The point is: check first your driver. That same issue mentioned:

I probably find the reason.
I updated my integrated graphics card driver, and everything is OK.
I do not know why, but now everything can work properly.

My device Info:

  • Intel(R) HD Graphics Family -> I updated this driver
  • AMD Radeon HD 8850M

Upvotes: 0

Related Questions