Commit 8c79e114 authored by Charles Chamberlain's avatar Charles Chamberlain

Using sdl to display blue window

parent 99a857b4
stars
stars_extension.o
stars_extension.d
libstars_extension.so
use "lib:GL"
use "lib:glut"
use @glutInit[None](argc: Pointer[U32], argv: Pointer[Pointer[U8]])
use @glutInitDisplayMode[None](mode: U32)
use @glutCreateWindow[None](name: Pointer[U8] tag)
use @glutMainLoop[None]()
use @glutSwapBuffers[None]()
use @glutDisplayFunc[None](f: @{(): None})
use @glutInitWindowSize[None](width: I32, height: I32)
use @glClear[None](kind: U32)
/* use @glVertex3fv[None](points: Array[F32]) */
use @glColor3f[None](r: F32, g: F32, b: F32)
use @glVertex3f[None](x: F32, y: F32, z: F32)
use @glBegin[None](kind: U32)
use @glEnd[None]()
interface HasDisplay
fun @display()
primitive Graphics
fun init(name: String, width: I32, height: I32) =>
let argv = Pointer[Pointer[U8]].create() // NULL
var argc = U32(0)
@glutInit[None](addressof argc, argv)
// GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH
@glutInitDisplayMode(18)
@glutInitWindowSize(width, height)
@glutCreateWindow(name.cstring())
fun loop(game: HasDisplay) =>
@glutDisplayFunc(addressof game.display)
@glutMainLoop()
fun swap() =>
@glutSwapBuffers()
fun clear() =>
// GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
@glClear(16640)
fun color(r: F32, g: F32, b: F32) =>
@glColor3f(r, g, b)
fun quad(x: F32, y: F32, width: F32, height: F32) =>
@glBegin(7) // GL_QUADS
@glVertex3f(x, y, 0)
@glVertex3f(x + width, y, 0)
@glVertex3f(x + width, y + height, 0)
@glVertex3f(x, y + height, 0)
@glEnd()
use "lib:SDL2"
use "lib:GLEW"
use "lib:GL" use "path:/home/charles/code/stars"
use "lib:stars_extension"
use @SDL_GL_SetAttribute[None](attr: U32, value: U32)
use @SDL_Init[I32](mode: U32)
use @SDL_GetError[Pointer[U8]]()
use @SDL_CreateWindow[Pointer[SDLWindow]](
name: Pointer[U8] tag,
x: U32,
y: U32,
width: U32,
height: U32,
mode: U32)
use @SDL_DestroyWindow[None](window: Pointer[SDLWindow] box)
use @SDL_Quit[None]()
use @SDL_PollEvent[U32](i: Pointer[SDLEvent])
use @SDL_GL_SwapWindow[None](window: Pointer[SDLWindow])
use @SDL_GL_CreateContext[Pointer[SDLWindow]](window: Pointer[SDLWindow])
use @glewInit[None]()
use @glClearColor[None](r: F32, g: F32, b: F32, a: F32)
use @glClear[None](mode: U32)
use @make_sdl_event[Pointer[SDLEvent]]()
use @is_sdl_quit_event[I32](event: Pointer[SDLEvent])
use @free_sdl_event[None](event: Pointer[SDLEvent])
primitive GLColorBufferBit fun apply(): U32 => 0x4000
primitive GLDepthBufferBit fun apply(): U32 => 0x100
primitive SDLProfileCore fun apply(): U32 => 1
primitive SDLMajorVersion fun apply(): U32 => 17
primitive SDLMinorVersion fun apply(): U32 => 18
primitive SDLProfileMask fun apply(): U32 => 21
primitive SDLWindowOpenGL fun apply(): U32 => 2
primitive SDLWindowShown fun apply(): U32 => 4
primitive SDLWindowPosCentered fun apply(): U32 => 0x2FFF0000
primitive SDLInitVideo fun apply(): U32 => 20
primitive SDLWindow
primitive SDLEvent
class Graphics
var _window: Pointer[SDLWindow] ref
var _env: Env
new create(env: Env) =>
_env = env
_window = Pointer[SDLWindow].create()
fun ref init( name: String, width: U32, height: U32) =>
@SDL_GL_SetAttribute(SDLMajorVersion(), 3)
@SDL_GL_SetAttribute(SDLMinorVersion(), 1)
@SDL_GL_SetAttribute(SDLProfileMask(), SDLProfileCore())
if @SDL_Init(SDLInitVideo()) < 0 then
_env.out.print("SDL could not be initialized")
print_error()
return
end
_window = @SDL_CreateWindow(
name.cstring(),
SDLWindowPosCentered(),
SDLWindowPosCentered(),
width,
height,
(SDLWindowOpenGL() or SDLWindowShown()))
if _window.is_null() then
_env.out.print("Window could not be created")
print_error()
return
end
let context = @SDL_GL_CreateContext(_window)
if context.is_null() then
_env.out.print("GL context could not be created")
print_error()
return
end
@glewInit()
fun print_error() =>
// we *do* have access to _env, but I'm
// not sure you can safely read returned cstrings.
@printf[None]("%s\n".cstring(), @SDL_GetError())
fun ref loop(f: {()}) =>
var event = @make_sdl_event()
while true do
while @SDL_PollEvent(event) != 0 do
if @is_sdl_quit_event(event) == 1 then return end
end
f()
@SDL_GL_SwapWindow(_window)
end
@free_sdl_event(event)
fun ref finish() =>
@SDL_DestroyWindow(_window)
_window = Pointer[SDLWindow].create()
@SDL_Quit()
build-pony: build-c
ponyc
build-c:
clang -fPIC -Wall -Wextra -O3 -g -MM stars_extension.c >stars_extension.d
clang -fPIC -Wall -Wextra -O3 -g -c -o stars_extension.o stars_extension.c
clang -shared -o libstars_extension.so stars_extension.o
clean:
rm -f stars_extension.d
rm -f stars_extension.o
rm -f libstars_extension.so
rm stars
// stars.pony
class Game
primitive Game
fun @display() =>
Graphics.clear()
Graphics.color(0.3,0.8,0.3)
Graphics.quad(0, 0, 0.1, 0.1)
Graphics.swap()
fun apply(env: Env) =>
@glClearColor(0.39, 0.58, 0.93, 1.0)
@glClear(GLColorBufferBit() or GLDepthBufferBit())
actor Main
new create(env: Env) =>
Graphics.init("Stars", 1000, 1000)
Graphics.loop(Game)
env.out.print("Hello world")
let graphics = Graphics.create(env)
graphics.init("Stars", 2000, 1000)
graphics.loop({()(env) => Game.apply(env)})
graphics.finish()
#include "./stars_extension.h"
SDL_Event *make_sdl_event()
{
return malloc(sizeof(SDL_Event));
}
int is_sdl_quit_event(SDL_Event *e)
{
return (*e).type == SDL_QUIT;
}
void free_sdl_event(SDL_Event *e)
{
free(e);
}
#include <SDL2/SDL.h>
#include <stdlib.h>
int is_sdl_quit_event(SDL_Event *e);
SDL_Event *make_sdl_event();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment