Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # OpenGL example using SDL2
- import sdl2
- import opengl/opengl
- import opengl/glu
- # Initialize SDL
- discard SDL_Init(INIT_EVERYTHING)
- var
- window = CreateWindow("SDL Skeleton", 100, 100, 640, 480, SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE)
- context = window.GL_CreateContext()
- # Initialize OpenGL
- proc Init_OpenGL() =
- loadExtensions()
- glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
- glClearDepth(1.0) # Set background depth to farthest
- glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
- glDepthFunc(GL_LEQUAL) # Set the type of depth-test
- glShadeModel(GL_SMOOTH) # Enable smooth shading
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Nice perspective corrections
- proc reshape(pos_x :float, pos_y :float) =
- # TODO: use real width and height instead of constant values
- glViewport(0, 0, 640, 480) # Set the viewport to cover the new window
- glMatrixMode(GL_PROJECTION) # To operate on the Projection matrix
- glLoadIdentity() # Reset
- # gluPerspective(40.0, 640 / 480, 0.1, 100.0) # Enable perspective projection with fovy, aspect, zNear and zFar
- gluPerspective(pos_y, 640 / 480, 0.1, 100.0)
- proc render() =
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
- glMatrixMode(GL_MODELVIEW) # To operate on model-view matrix
- glLoadIdentity() # Reset the model-view matrix
- glTranslatef(1.5, 0.0, -7.0) # Move right and into the screen
- # Render a cube consisting of 6 quads
- # Each quad consists of 2 triangles
- # Each triangle consists of 3 vertices
- glBegin(GL_TRIANGLES) # Begin drawing of triangles
- # Top face (y = 1.0f)
- glColor3f(0.0, 1.0, 0.0) # Green
- glVertex3f( 1.0, 1.0, -1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f( 1.0, 1.0, 1.0)
- glVertex3f( 1.0, 1.0, -1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- # Bottom face (y = -1.0f)
- glColor3f(1.0, 0.5, 0.0) # Orange
- glVertex3f( 1.0, -1.0, 1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f( 1.0, -1.0, -1.0)
- glVertex3f( 1.0, -1.0, 1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- # Front face (z = 1.0f)
- glColor3f(1.0, 0.0, 0.0) # Red
- glVertex3f( 1.0, 1.0, 1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- glVertex3f( 1.0, -1.0, 1.0)
- glVertex3f( 1.0, 1.0, 1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- # Back face (z = -1.0f)
- glColor3f(1.0, 1.0, 0.0) # Yellow
- glVertex3f( 1.0, -1.0, -1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f( 1.0, 1.0, -1.0)
- glVertex3f( 1.0, -1.0, -1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- # Left face (x = -1.0f)
- glColor3f(0.0, 0.0, 1.0) # Blue
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f(-1.0, 1.0, -1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- glVertex3f(-1.0, -1.0, 1.0)
- glVertex3f(-1.0, 1.0, 1.0)
- glVertex3f(-1.0, -1.0, -1.0)
- # Right face (x = 1.0f)
- glColor3f(1.0, 0.0, 1.0) # Magenta
- glVertex3f(1.0, 1.0, -1.0)
- glVertex3f(1.0, 1.0, 1.0)
- glVertex3f(1.0, -1.0, 1.0)
- glVertex3f(1.0, -1.0, -1.0)
- glVertex3f(1.0, 1.0, -1.0)
- glVertex3f(1.0, -1.0, 1.0)
- glEnd() # End of drawing
- window.GL_SwapWindow() # Swap the front and back frame buffers (double buffering)
- const
- RELEASED = 0
- PRESSED = 1
- let
- MAXFRAMERATE: uint32 = 20 # milli seconds
- var
- frametime :uint32
- evt :TEvent
- runGame :bool = true
- position :float = 20
- key_state :ptr array[0 .. SDL_NUM_SCANCODES.int, uint8]
- tmp :cstring
- # Framerate limiter
- proc limitFramerate() =
- var now = GetTicks()
- if frametime > now:
- Delay(frametime - now)
- frametime = frametime + MAXFRAMERATE
- #Initialize OpenGL
- Init_OpenGL()
- #### ERROR
- tmp = cast[cstring](glGetString(GL_SHADING_LANGUAGE_VERSION))
- echo(tmp)
- # Main loop
- while runGame:
- while PollEvent(evt):
- if evt.kind == QuitEvent:
- runGame = false
- break
- if evt.kind == WindowEvent:
- reshape(pos_x=0.0, pos_y=position)
- #Checking the keyboard state
- key_state = GetKeyboardState()
- if key_state[SDL_SCANCODE_UP.ord] == PRESSED:
- reshape(pos_x=0.0, pos_y=position)
- if position < 100:
- position += 1
- elif key_state[SDL_SCANCODE_DOWN.ord] == PRESSED:
- reshape(pos_x=0.0, pos_y=position)
- if position > 10:
- position -= 1
- elif key_state[SDL_SCANCODE_ESCAPE.ord] == PRESSED:
- rungame = false
- render()
- limitFramerate()
- destroy window
Advertisement
Add Comment
Please, Sign In to add comment