1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sdl: separate sdl module into multiple, dependent submodules

This commit is contained in:
prime31 2019-12-08 12:30:38 -08:00 committed by Alexander Medvednikov
parent 9730164613
commit faedebbb4e
8 changed files with 391 additions and 119 deletions

View File

@ -11,12 +11,12 @@ jobs:
uses: actions/checkout@v1 uses: actions/checkout@v1
- name: Build V - name: Build V
uses: spytheman/docker_alpine_v@v5.0 uses: spytheman/docker_alpine_v@v6.0
with: with:
entrypoint: .github/workflows/alpine.build.sh entrypoint: .github/workflows/alpine.build.sh
- name: Test V - name: Test V
uses: spytheman/docker_alpine_v@v5.0 uses: spytheman/docker_alpine_v@v6.0
with: with:
entrypoint: .github/workflows/alpine.test.sh entrypoint: .github/workflows/alpine.test.sh
@ -29,7 +29,7 @@ jobs:
node-version: 12.x node-version: 12.x
- name: Install dependencies - name: Install dependencies
run: | run: |
brew install freetype glfw openssl postgres brew install freetype glfw openssl postgres sdl2 sdl2_ttf sdl2_mixer sdl2_image
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/" export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/"
- name: Build V - name: Build V
run: make && ./v -o v v.v run: make && ./v -o v v.v
@ -67,7 +67,7 @@ jobs:
with: with:
node-version: 12.x node-version: 12.x
- name: Install dependencies - name: Install dependencies
run: sudo apt-get update; sudo apt-get install --quiet -y postgresql libpq-dev libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev run: sudo apt-get update; sudo apt-get install --quiet -y postgresql libpq-dev libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build V - name: Build V
run: make && ./v -cc gcc -o v v.v run: make && ./v -cc gcc -o v v.v
- name: Test V - name: Test V
@ -88,7 +88,7 @@ jobs:
runs-on: ubuntu-18.04 runs-on: ubuntu-18.04
steps: steps:
- name: Install dependencies - name: Install dependencies
run: sudo apt-get update; sudo apt-get install --quiet -y postgresql libpq-dev libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev run: sudo apt-get update; sudo apt-get install --quiet -y postgresql libpq-dev libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Download V - name: Download V
run: wget https://github.com/vlang/v/releases/latest/download/v_linux.zip && unzip v_linux.zip && ./v --version run: wget https://github.com/vlang/v/releases/latest/download/v_linux.zip && unzip v_linux.zip && ./v --version
- name: Test V - name: Test V
@ -101,7 +101,7 @@ jobs:
steps: steps:
- name: Install dependencies - name: Install dependencies
run: | run: |
brew install freetype glfw openssl brew install freetype glfw openssl sdl2 sdl2_ttf sdl2_mixer sdl2_image
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/" export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/"
- name: Download V - name: Download V
run: wget https://github.com/vlang/v/releases/latest/download/v_macos.zip && unzip v_macos.zip && ./v --version run: wget https://github.com/vlang/v/releases/latest/download/v_macos.zip && unzip v_macos.zip && ./v --version
@ -124,7 +124,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Install dependencies - name: Install dependencies
run: sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev run: sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build v - name: Build v
run: echo $VFLAGS && make && ./v -o v v.v run: echo $VFLAGS && make && ./v -o v v.v
- name: Test v->c - name: Test v->c
@ -146,7 +146,7 @@ jobs:
with: with:
node-version: 12.x node-version: 12.x
- name: Install dependencies - name: Install dependencies
run: sudo apt-get update; sudo apt-get install --quiet -y musl musl-tools run: sudo apt-get update; sudo apt-get install --quiet -y musl musl-tools libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build v - name: Build v
run: echo $VFLAGS && make && ./v -o v v.v run: echo $VFLAGS && make && ./v -o v v.v
- name: Test v binaries - name: Test v binaries

View File

@ -15,4 +15,6 @@ RUN apk --no-cache add \
openssl-dev sqlite-dev \ openssl-dev sqlite-dev \
libx11-dev glfw-dev freetype-dev libx11-dev glfw-dev freetype-dev
RUN apk --no-cache add --virtual sdl2deps sdl2-dev sdl2_ttf-dev sdl2_mixer-dev sdl2_image-dev
RUN git clone https://github.com/vlang/v /opt/vlang && make && v --version RUN git clone https://github.com/vlang/v /opt/vlang && make && v --version

View File

@ -0,0 +1,31 @@
module main
import sdl
fn main() {
C.SDL_Init(C.SDL_INIT_VIDEO)
window := C.SDL_CreateWindow('Hello SDL2', 300, 300, 500, 300, 0)
renderer := C.SDL_CreateRenderer(window, -1, C.SDL_RENDERER_ACCELERATED | C.SDL_RENDERER_PRESENTVSYNC)
mut should_close := false
for {
ev := sdl.Event{}
for 0 < sdl.poll_event(&ev) {
match int(ev._type) {
C.SDL_QUIT { should_close = true }
else {}
}
}
if should_close {
break
}
C.SDL_SetRenderDrawColor(renderer, 255, 55, 55, 255)
C.SDL_RenderClear(renderer)
C.SDL_RenderPresent(renderer)
}
C.SDL_DestroyRenderer(renderer)
C.SDL_DestroyWindow(window)
C.SDL_Quit()
}

View File

@ -13,7 +13,9 @@ import os
import math import math
import sdl import sdl
import sdl.image as img import sdl.image as img
[inline] fn sdl_fill_rect(s &sdl.Surface,r &sdl.Rect,c &sdl.Color){sdl.fill_rect(s,r,c)} import sdl.mixer as mix
import sdl.ttf as ttf
[inline] fn sdl_fill_rect(s &SDL_Surface,r &SDL_Rect,c &SDL_Color){sdl.fill_rect(s,r,c)}
const ( const (
Title = 'tVintris' Title = 'tVintris'
@ -68,6 +70,11 @@ const (
JHP2RIGHT = 2 JHP2RIGHT = 2
) )
const (
mix_version = mix.version
ttf_version = ttf.version
)
const ( const (
// Tetros' 4 possible states are encoded in binaries // Tetros' 4 possible states are encoded in binaries
BTetros = [ BTetros = [
@ -107,25 +114,22 @@ const (
] ]
// Each tetro has its unique color // Each tetro has its unique color
Colors = [ Colors = [
sdl.Color{byte(0), byte(0), byte(0), byte(0)}, // unused ? SDL_Color{byte(0), byte(0), byte(0), byte(0)}, // unused ?
sdl.Color{byte(0), byte(0x62), byte(0xc0), byte(0)}, // quad : darkblue 0062c0 SDL_Color{byte(0), byte(0x62), byte(0xc0), byte(0)}, // quad : darkblue 0062c0
sdl.Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}, // tricorn : lightbrown ca7d5f SDL_Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}, // tricorn : lightbrown ca7d5f
sdl.Color{byte(0), byte(0xc1), byte(0xbf), byte(0)}, // short topright : lightblue 00c1bf SDL_Color{byte(0), byte(0xc1), byte(0xbf), byte(0)}, // short topright : lightblue 00c1bf
sdl.Color{byte(0), byte(0xc1), byte(0), byte(0)}, // short topleft : lightgreen 00c100 SDL_Color{byte(0), byte(0xc1), byte(0), byte(0)}, // short topleft : lightgreen 00c100
sdl.Color{byte(0xbf), byte(0xbe), byte(0), byte(0)}, // long topleft : yellowish bfbe00 SDL_Color{byte(0xbf), byte(0xbe), byte(0), byte(0)}, // long topleft : yellowish bfbe00
sdl.Color{byte(0xd1), byte(0), byte(0xbf), byte(0)}, // long topright : pink d100bf SDL_Color{byte(0xd1), byte(0), byte(0xbf), byte(0)}, // long topright : pink d100bf
sdl.Color{byte(0xd1), byte(0), byte(0), byte(0)}, // longest : lightred d10000 SDL_Color{byte(0xd1), byte(0), byte(0), byte(0)}, // longest : lightred d10000
sdl.Color{byte(0), byte(170), byte(170), byte(0)}, // unused ? SDL_Color{byte(0), byte(170), byte(170), byte(0)}, // unused ?
] ]
// Background color // Background color
BackgroundColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)} BackgroundColor = SDL_Color{byte(0), byte(0), byte(0), byte(0)}
// BackgroundColor = sdl.Color{byte(255), byte(255), byte(255), byte(0)}
// Foreground color // Foreground color
ForegroundColor = sdl.Color{byte(0), byte(170), byte(170), byte(0)} ForegroundColor = SDL_Color{byte(0), byte(170), byte(170), byte(0)}
// ForegroundColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)}
// Text color // Text color
TextColor = sdl.Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)} TextColor = SDL_Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}
// TextColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)}
) )
// TODO: type Tetro [TetroSize]struct{ x, y int } // TODO: type Tetro [TetroSize]struct{ x, y int }
@ -154,7 +158,7 @@ mut:
h int h int
window voidptr window voidptr
renderer voidptr renderer voidptr
screen &sdl.Surface screen &SDL_Surface
texture voidptr texture voidptr
// AUDIO // AUDIO
actx AudioContext actx AudioContext
@ -162,7 +166,7 @@ mut:
jnames [2]string jnames [2]string
jids [2]int jids [2]int
// V logo // V logo
v_logo &sdl.Surface v_logo &SDL_Surface
tv_logo voidptr tv_logo voidptr
} }
@ -238,7 +242,7 @@ fn (sdlc mut SdlContext) set_sdl_context(w int, h int, title string) {
sdlc.screen = sdl.create_rgb_surface(0, w, h, bpp, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000) sdlc.screen = sdl.create_rgb_surface(0, w, h, bpp, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)
sdlc.texture = C.SDL_CreateTexture(sdlc.renderer, C.SDL_PIXELFORMAT_ARGB8888, C.SDL_TEXTUREACCESS_STREAMING, w, h) sdlc.texture = C.SDL_CreateTexture(sdlc.renderer, C.SDL_PIXELFORMAT_ARGB8888, C.SDL_TEXTUREACCESS_STREAMING, w, h)
C.Mix_Init(0) C.Mix_Init(C.MIX_INIT_MOD)
C.atexit(C.Mix_Quit) C.atexit(C.Mix_Quit)
if C.Mix_OpenAudio(48000,C.MIX_DEFAULT_FORMAT,2,AudioBufSize) < 0 { if C.Mix_OpenAudio(48000,C.MIX_DEFAULT_FORMAT,2,AudioBufSize) < 0 {
println('couldn\'t open audio') println('couldn\'t open audio')
@ -375,7 +379,7 @@ fn main() {
match int(ev._type) { match int(ev._type) {
C.SDL_QUIT { should_close = true } C.SDL_QUIT { should_close = true }
C.SDL_KEYDOWN { C.SDL_KEYDOWN {
key := int(ev.key.keysym.sym) key := ev.key.keysym.sym
if key == C.SDLK_ESCAPE { if key == C.SDLK_ESCAPE {
should_close = true should_close = true
break break
@ -385,7 +389,7 @@ fn main() {
} }
C.SDL_JOYBUTTONDOWN { C.SDL_JOYBUTTONDOWN {
jb := int(ev.jbutton.button) jb := int(ev.jbutton.button)
joyid := int(ev.jbutton.which) joyid := ev.jbutton.which
// println('JOY BUTTON $jb $joyid') // println('JOY BUTTON $jb $joyid')
game.handle_jbutton(jb, joyid) game.handle_jbutton(jb, joyid)
game2.handle_jbutton(jb, joyid) game2.handle_jbutton(jb, joyid)
@ -393,7 +397,7 @@ fn main() {
C.SDL_JOYHATMOTION { C.SDL_JOYHATMOTION {
jh := int(ev.jhat.hat) jh := int(ev.jhat.hat)
jv := int(ev.jhat.value) jv := int(ev.jhat.value)
joyid := int(ev.jhat.which) joyid := ev.jhat.which
// println('JOY HAT $jh $jv $joyid') // println('JOY HAT $jh $jv $joyid')
game.handle_jhat(jh, jv, joyid) game.handle_jhat(jh, jv, joyid)
game2.handle_jhat(jh, jv, joyid) game2.handle_jhat(jh, jv, joyid)
@ -442,7 +446,7 @@ enum Action {
} }
fn (game mut Game) handle_key(key int) { fn (game mut Game) handle_key(key int) {
// global keys // global keys
mut action := Action(.idle) mut action := Action.idle
match key { match key {
C.SDLK_SPACE { action = .space } C.SDLK_SPACE { action = .space }
game.k_fire { action = .fire } game.k_fire { action = .fire }
@ -488,7 +492,7 @@ fn (game mut Game) handle_jbutton(jb int, joyid int) {
return return
} }
// global buttons // global buttons
mut action := Action(.idle) mut action := Action.idle
match jb { match jb {
game.jb_fire { action = .fire } game.jb_fire { action = .fire }
else {} else {}
@ -708,7 +712,7 @@ fn (g &Game) draw_tetro() {
} }
fn (g &Game) draw_block(i, j, color_idx int) { fn (g &Game) draw_block(i, j, color_idx int) {
rect := sdl.Rect {g.ofs_x + (j - 1) * BlockSize, (i - 1) * BlockSize, rect := SDL_Rect {g.ofs_x + (j - 1) * BlockSize, (i - 1) * BlockSize,
BlockSize - 1, BlockSize - 1} BlockSize - 1, BlockSize - 1}
col := Colors[color_idx] col := Colors[color_idx]
sdl_fill_rect(g.sdl.screen, &rect, &col) sdl_fill_rect(g.sdl.screen, &rect, &col)
@ -732,27 +736,27 @@ fn (g &Game) draw_v_logo() {
texw := 0 texw := 0
texh := 0 texh := 0
C.SDL_QueryTexture(g.sdl.tv_logo, 0, 0, &texw, &texh) C.SDL_QueryTexture(g.sdl.tv_logo, 0, 0, &texw, &texh)
dstrect := sdl.Rect { (WinWidth / 2) - (texw / 2), 20, texw, texh } dstrect := SDL_Rect { (WinWidth / 2) - (texw / 2), 20, texw, texh }
// Currently we can't seem to use sdl.render_copy when we need to pass a nil pointer (eg: srcrect to be NULL) // Currently we can't seem to use sdl.render_copy when we need to pass a nil pointer (eg: srcrect to be NULL)
// sdl.render_copy(g.sdl.renderer, tv_logo, 0, &dstrect) // sdl.render_copy(g.sdl.renderer, tv_logo, 0, &dstrect)
C.SDL_RenderCopy(g.sdl.renderer, g.sdl.tv_logo, voidptr(0), voidptr(&dstrect)) C.SDL_RenderCopy(g.sdl.renderer, g.sdl.tv_logo, voidptr(0), voidptr(&dstrect))
} }
fn (g &Game) draw_text(x int, y int, text string, tcol sdl.Color) { fn (g &Game) draw_text(x int, y int, text string, tcol SDL_Color) {
_tcol := C.SDL_Color{tcol.r, tcol.g, tcol.b, tcol.a} _tcol := C.SDL_Color{tcol.r, tcol.g, tcol.b, tcol.a}
tsurf := C.TTF_RenderText_Solid(g.font, text.str, _tcol) tsurf := C.TTF_RenderText_Solid(g.font, text.str, _tcol)
ttext := C.SDL_CreateTextureFromSurface(g.sdl.renderer, tsurf) ttext := C.SDL_CreateTextureFromSurface(g.sdl.renderer, tsurf)
texw := 0 texw := 0
texh := 0 texh := 0
C.SDL_QueryTexture(ttext, 0, 0, &texw, &texh) C.SDL_QueryTexture(ttext, 0, 0, &texw, &texh)
dstrect := sdl.Rect { x, y, texw, texh } dstrect := SDL_Rect { x, y, texw, texh }
// sdl.render_copy(g.sdl.renderer, ttext, 0, &dstrect) // sdl.render_copy(g.sdl.renderer, ttext, 0, &dstrect)
C.SDL_RenderCopy(g.sdl.renderer, ttext, voidptr(0), voidptr(&dstrect)) C.SDL_RenderCopy(g.sdl.renderer, ttext, voidptr(0), voidptr(&dstrect))
C.SDL_DestroyTexture(ttext) C.SDL_DestroyTexture(ttext)
sdl.free_surface(tsurf) sdl.free_surface(tsurf)
} }
[inline] fn (g &Game) draw_ptext(x int, y int, text string, tcol sdl.Color) { [inline] fn (g &Game) draw_ptext(x int, y int, text string, tcol SDL_Color) {
g.draw_text(g.ofs_x + x, y, text, tcol) g.draw_text(g.ofs_x + x, y, text, tcol)
} }
@ -760,14 +764,14 @@ fn (g &Game) draw_text(x int, y int, text string, tcol sdl.Color) {
fn (g &Game) draw_begin() { fn (g &Game) draw_begin() {
// println('about to clear') // println('about to clear')
C.SDL_RenderClear(g.sdl.renderer) C.SDL_RenderClear(g.sdl.renderer)
mut rect := sdl.Rect {0,0,g.sdl.w,g.sdl.h} mut rect := SDL_Rect {0,0,g.sdl.w,g.sdl.h}
col := sdl.Color{byte(00), byte(00), byte(0), byte(0)} col := SDL_Color{byte(00), byte(00), byte(0), byte(0)}
// sdl_fill_rect(g.sdl.screen, &rect, BackgroundColor) // sdl_fill_rect(g.sdl.screen, &rect, BackgroundColor)
sdl_fill_rect(g.sdl.screen, &rect, col) sdl_fill_rect(g.sdl.screen, &rect, col)
rect = sdl.Rect {BlockSize * FieldWidth + 2,0,2,g.sdl.h} rect = SDL_Rect {BlockSize * FieldWidth + 2,0,2,g.sdl.h}
sdl_fill_rect(g.sdl.screen, &rect, ForegroundColor) sdl_fill_rect(g.sdl.screen, &rect, ForegroundColor)
rect = sdl.Rect {WinWidth - BlockSize * FieldWidth - 4,0,2,g.sdl.h} rect = SDL_Rect {WinWidth - BlockSize * FieldWidth - 4,0,2,g.sdl.h}
sdl_fill_rect(g.sdl.screen, &rect, ForegroundColor) sdl_fill_rect(g.sdl.screen, &rect, ForegroundColor)
mut idx := 0 mut idx := 0
@ -778,7 +782,7 @@ fn (g &Game) draw_begin() {
} }
w := BlockSize w := BlockSize
h := s * 4 * w / 100 h := s * 4 * w / 100
rect = sdl.Rect {(WinWidth - 7 * (w + 1)) / 2 + idx * (w + 1), WinHeight * 3 / 4 - h, w, h} rect = SDL_Rect {(WinWidth - 7 * (w + 1)) / 2 + idx * (w + 1), WinHeight * 3 / 4 - h, w, h}
sdl_fill_rect(g.sdl.screen, &rect, Colors[idx + 1]) sdl_fill_rect(g.sdl.screen, &rect, Colors[idx + 1])
idx++ idx++
} }
@ -831,7 +835,7 @@ fn parse_binary_tetro(t_ int) []Block {
for i := 0; i <= 3; i++ { for i := 0; i <= 3; i++ {
// Get ith digit of t // Get ith digit of t
p := int(math.pow(10, 3 - i)) p := int(math.pow(10, 3 - i))
mut digit := int(t / p) mut digit := t / p
t %= p t %= p
// Convert the digit to binary // Convert the digit to binary
for j := 3; j >= 0; j-- { for j := 3; j >= 0; j-- {

View File

@ -1,4 +1,5 @@
module image module image
import sdl
#flag linux -lSDL2_image #flag linux -lSDL2_image
#include <SDL_image.h> #include <SDL_image.h>
@ -10,10 +11,43 @@ module image
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
// SDL_Image.h // SDL_Image.h
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
//fn C.IMG_Load_RW(logo &sdl.RwOps, free_src int) &sdl.Surface pub const (
IMG_INIT_JPG = 0x00000001
IMG_INIT_PNG = 0x00000002
IMG_INIT_TIF = 0x00000004
IMG_INIT_WEBP = 0x00000008
)
fn C.IMG_Init(flags int) int fn C.IMG_Init(flags int) int
fn C.IMG_Quit() fn C.IMG_Quit()
fn C.IMG_Load(file byteptr) voidptr
/* Load an image from an SDL data source. The 'type' may be one of: "BMP", "GIF", "PNG", etc. */
fn C.IMG_LoadTyped_RW(src &SDL_RWops, freesrc int, _type byteptr) &SDL_Surface
fn C.IMG_Load(file byteptr) &SDL_Surface
fn C.IMG_Load_RW(src &SDL_RWops, freesrc int) &SDL_Surface
/* Load an image directly into a render texture. */
fn C.IMG_LoadTexture(renderer &SDL_Renderer, file byteptr) &SDL_Texture
fn C.IMG_LoadTexture_RW(renderer &SDL_Renderer, src &SDL_RWops, freesrc int) &SDL_Texture
fn C.IMG_LoadTextureTyped_RW(renderer &SDL_Renderer, src &SDL_RWops, freesrc int, _type byteptr) &SDL_Texture
/* Functions to detect a file type, given a seekable source */
fn C.IMG_isPNG(src &SDL_RWops) int
fn C.IMG_isBMP(src &SDL_RWops) int
fn C.IMG_isJPG(src &SDL_RWops) int
fn C.IMG_isWEBP(src &SDL_RWops) int
/* Individual loading functions */
fn C.IMG_LoadPNG_RW(src &SDL_RWops) &SDL_Surface
fn C.IMG_LoadBMP_RW(src &SDL_RWops) &SDL_Surface
fn C.IMG_LoadJPG_RW(src &SDL_RWops) &SDL_Surface
fn C.IMG_LoadWEBP_RW(src &SDL_RWops) &SDL_Surface
/* Individual saving functions */
fn C.IMG_SavePNG(surface voidptr, file byteptr) int
fn C.IMG_SavePNG_RW(surface voidptr, dst &SDL_RWops, freedst int) int
fn C.IMG_SaveJPG(surface voidptr, file byteptr) int
fn C.IMG_SaveJPG_RW(surface voidptr, dst &SDL_RWops, freedst int) int
pub fn img_init(flags int) int { pub fn img_init(flags int) int {
return C.IMG_Init(flags) return C.IMG_Init(flags)
@ -23,7 +57,11 @@ pub fn quit() {
C.IMG_Quit() C.IMG_Quit()
} }
pub fn load(file string) &sdl.Surface { pub fn load(file string) &SDL_Surface {
res := C.IMG_Load(file.str) res := C.IMG_Load(file.str)
return res return res
} }
pub const (
version = sdl.version // TODO: remove this hack to mark sdl as used; avoids warning
)

101
vlib/sdl/mixer/mixer.v Normal file
View File

@ -0,0 +1,101 @@
module mixer
import sdl
#include <SDL_mixer.h>
pub const (
MIX_CHANNEL_POST = -2
MIX_MAX_VOLUME = C.MIX_MAX_VOLUME
MIX_CHANNELS = 8
MIX_DEFAULT_FREQUENCY = 22050
MIX_DEFAULT_FORMAT = C.MIX_DEFAULT_FORMAT
MIX_INIT_FLAC = 0x00000001
MIX_INIT_MOD = 0x00000002
MIX_INIT_MP3 = 0x00000008
MIX_INIT_OGG = 0x00000010
MIX_INIT_MID = 0x00000020
MIX_INIT_OPUS = 0x00000040
)
// Structs
// MIX TODO: get this working as a return type
pub struct C.Mix_Chunk {
allocated int
abuf &byte // *UInt8
alen u32
volume byte /* Per-sample volume, 0-128 */
}
pub struct C.Mix_Music {}
// Methods
// MIX
fn C.Mix_Init(flags int) int
fn C.Mix_OpenAudio(frequency int, format u16, channels int, chunksize int) int
fn C.Mix_CloseAudio()
fn C.Mix_LoadMUS(file byteptr) voidptr // *Mix_Music
fn C.Mix_LoadMUS_RW(src &SDL_RWops, freesrc int) voidptr // *Mix_Music
fn C.Mix_LoadWAV(file byteptr) voidptr // *Mix_Chunk
fn C.Mix_LoadWAV_RW(src &SDL_RWops, freesrc int) voidptr // *Mix_Chunk
// Music
fn C.Mix_FadeInMusic(music &Mix_Music, loops int, ms int) int
fn C.Mix_PlayMusic(music &SDL_AudioSpec, loops int) int
fn C.Mix_VolumeMusic(volume int) int
fn C.Mix_PauseMusic()
fn C.Mix_ResumeMusic()
fn C.Mix_RewindMusic()
fn C.Mix_SetMusicPosition(position f64) int
fn C.Mix_PausedMusic() int
fn C.Mix_HaltMusic() int
fn C.Mix_FadeOutMusic(ms int) int
fn C.Mix_HookMusicFinished(cb fn())
fn C.Mix_FreeMusic(music &Mix_Music)
// Channels
fn C.Mix_VolumeChunk(chunk &Mix_Chunk, volume int) int
fn C.Mix_PlayChannel(channel int, chunk &Mix_Chunk, loops int) int
fn C.Mix_FadeInChannel(channel int, chunk &Mix_Chunk, loops int, ms int) int
fn C.Mix_PlayChannelTimed(channel int, chunk &Mix_Chunk, loops int, ticks int) int
fn C.Mix_Pause(channel int)
fn C.Mix_Resume(channel int)
fn C.Mix_HaltChannel(channel int) int
fn C.Mix_ExpireChannel(channel int, ticks int) int
fn C.Mix_FadeOutChannel(channel int, ms int) int
fn C.Mix_ChannelFinished(cb fn (int))
fn C.Mix_Playing(channel int) int
fn C.Mix_Paused(channel int) int
fn C.Mix_GetChunk(channel int) voidptr //Mix_Chunk
fn C.Mix_FreeChunk(chunk &Mix_Chunk)
fn C.Mix_ReserveChannels(num int) int
// Groups
fn C.Mix_GroupChannel(which int, tag int) int
fn C.Mix_GroupChannels(from int, to int, tag int) int
fn C.Mix_GroupAvailable(tag int) int
fn C.Mix_GroupCount(tag int) int
fn C.Mix_GroupOldest(tag int) int
fn C.Mix_GroupNewer(tag int) int
fn C.Mix_FadeOutGroup(tag int, ms int) int
fn C.Mix_HaltGroup(tag int) int
// Effects
type EffectFunc fn (int, voidptr, int, voidptr) // int chan, void *stream, int len, void *udata
type EffectDone fn (int, voidptr) // int chan, void *udata
fn C.Mix_RegisterEffect(channel int, f EffectFunc, d EffectDone, arg voidptr) int
fn C.Mix_UnregisterEffect(channel int, f EffectFunc) int
fn C.Mix_UnregisterAllEffects(channel int) int
fn C.Mix_SetPanning(channel int, left byte, right byte) int
fn C.Mix_SetDistance(channel int, distance byte) int
fn C.Mix_SetPosition(channel int, angle i16, distance byte) int
fn C.Mix_SetReverseStereo(channel int, flip int) int
pub const (
version = sdl.version // TODO: remove this hack to mark sdl as used; avoids warning
)

View File

@ -19,19 +19,12 @@ module sdl
#flag windows -L/mingw64/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_image #flag windows -L/mingw64/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_mixer -lSDL2_image
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
//struct C.SDL_Color{ pub struct C.SDL_RWops {}
pub struct Color{ pub struct C.SDL_Window {}
pub: pub struct C.SDL_Renderer {}
r byte /**< Red value 0-255 */ pub struct C.SDL_Texture {}
g byte /**< Green value 0-255 */
b byte /**< Blue value 0-255 */
a byte /**< Alpha value 0-255 */
}
//type Color C.SDL_Color
pub struct C.SDL_Color{ pub struct C.SDL_Color{
pub: pub:
@ -41,18 +34,15 @@ pub:
a byte a byte
} }
//struct C.SDL_Rect { pub struct C.SDL_Rect {
pub struct Rect { pub mut:
pub: x int
x int /**< number of pixels from left side of screen */ y int
y int /**< num of pixels from top of screen */ w int
w int /**< width of rectangle */ h int
h int /**< height of rectangle */
} }
//type Rect C.SDL_Rect
//pub struct C.SDL_Surface { pub struct C.SDL_Surface {
pub struct Surface {
pub: pub:
flags u32 flags u32
format voidptr format voidptr
@ -63,12 +53,11 @@ pub:
userdata voidptr userdata voidptr
locked int locked int
lock_data voidptr lock_data voidptr
clip_rect Rect clip_rect SDL_Rect
map voidptr map voidptr
refcount int refcount int
} }
//type Surface C.SDL_Surface
//type Surface Surface
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
@ -116,7 +105,6 @@ pub:
*/ */
} }
//pub union EventU {
pub union Event { pub union Event {
pub: pub:
_type u32 _type u32
@ -126,13 +114,10 @@ pub:
jhat JoyHatEvent jhat JoyHatEvent
_pad56 [56]byte _pad56 [56]byte
} }
//type Event EventU
//struct C.SDL_AudioSpec { pub struct C.SDL_AudioSpec {
pub struct AudioSpec { pub mut:
pub:
mut:
freq int /**< DSP frequency -- samples per second */ freq int /**< DSP frequency -- samples per second */
format u16 /**< Audio data format */ format u16 /**< Audio data format */
channels byte /**< Number of channels: 1 mono, 2 stereo */ channels byte /**< Number of channels: 1 mono, 2 stereo */
@ -166,8 +151,10 @@ fn C.SDL_NumJoysticks() int
fn C.SDL_JoystickNameForIndex(device_index int) voidptr fn C.SDL_JoystickNameForIndex(device_index int) voidptr
fn C.SDL_RenderCopy(renderer voidptr, texture voidptr, srcrect voidptr, dstrect voidptr) int fn C.SDL_RenderCopy(renderer voidptr, texture voidptr, srcrect voidptr, dstrect voidptr) int
fn C.SDL_CreateWindow(title byteptr, x int, y int, w int, h int, flags u32) voidptr fn C.SDL_CreateWindow(title byteptr, x int, y int, w int, h int, flags u32) voidptr
fn C.SDL_CreateRenderer(window &SDL_Window, index int, flags u32) voidptr
fn C.SDL_CreateWindowAndRenderer(width int, height int, window_flags u32, window &voidptr, renderer &voidptr) int fn C.SDL_CreateWindowAndRenderer(width int, height int, window_flags u32, window &voidptr, renderer &voidptr) int
fn C.SDL_DestroyWindow(window voidptr) fn C.SDL_DestroyWindow(window voidptr)
fn C.SDL_DestroyRenderer(renderer voidptr)
fn C.SDL_GetWindowSize(window voidptr, w voidptr, h voidptr) fn C.SDL_GetWindowSize(window voidptr, w voidptr, h voidptr)
fn C.SDL_SetHint(name byteptr, value byteptr) C.SDL_bool fn C.SDL_SetHint(name byteptr, value byteptr) C.SDL_bool
//fn C.SDL_RWFromFile(byteptr, byteptr) &RwOps //fn C.SDL_RWFromFile(byteptr, byteptr) &RwOps
@ -175,6 +162,7 @@ fn C.SDL_SetHint(name byteptr, value byteptr) C.SDL_bool
fn C.SDL_CreateTextureFromSurface(renderer voidptr, surface voidptr) voidptr fn C.SDL_CreateTextureFromSurface(renderer voidptr, surface voidptr) voidptr
fn C.SDL_CreateTexture(renderer voidptr, format u32, access int, w int, h int) voidptr fn C.SDL_CreateTexture(renderer voidptr, format u32, access int, w int, h int) voidptr
fn C.SDL_FillRect(dst voidptr, dstrect voidptr, color u32) int fn C.SDL_FillRect(dst voidptr, dstrect voidptr, color u32) int
fn C.SDL_SetRenderDrawColor(renderer voidptr, r byte, g byte, b byte, a byte)
fn C.SDL_RenderPresent(renderer voidptr) fn C.SDL_RenderPresent(renderer voidptr)
fn C.SDL_RenderClear(renderer voidptr) int fn C.SDL_RenderClear(renderer voidptr) int
fn C.SDL_UpdateTexture(texture voidptr, rect voidptr, pixels voidptr, pitch int) int fn C.SDL_UpdateTexture(texture voidptr, rect voidptr, pixels voidptr, pitch int) int
@ -203,33 +191,6 @@ fn C.SDL_GetPerformanceCounter() u64
fn C.SDL_GetPerformanceFrequency() u64 fn C.SDL_GetPerformanceFrequency() u64
fn C.SDL_Delay(ms u32) fn C.SDL_Delay(ms u32)
//////////////////////////////////////////////////////////
// TTF
//////////////////////////////////////////////////////////
fn C.TTF_Init() int
fn C.TTF_Quit()
fn C.TTF_OpenFont(file byteptr, ptsize int) voidptr
fn C.TTF_CloseFont(font voidptr)
//fn C.TTF_RenderText_Solid(voidptr, voidptr, SdlColor) voidptr
fn C.TTF_RenderText_Solid(voidptr, voidptr, C.SDL_Color) voidptr
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// MIX
//////////////////////////////////////////////////////////
fn C.Mix_Init(flags int) int
fn C.Mix_OpenAudio(frequency int, format u16, channels int, chunksize int) int
fn C.Mix_LoadMUS(file byteptr) voidptr
fn C.Mix_LoadWAV(file byteptr) voidptr
fn C.Mix_PlayMusic(music voidptr, loops int) int
fn C.Mix_VolumeMusic(volume int) int
fn C.Mix_FreeMusic(music voidptr)
fn C.Mix_CloseAudio()
fn C.Mix_FreeChunk(chunk voidptr)
fn C.Mix_PauseMusic()
fn C.Mix_ResumeMusic()
fn C.Mix_PlayChannel(channel int, chunk voidptr, loops int) int
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
// GL // GL
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
@ -240,7 +201,7 @@ fn C.SDL_GL_SetSwapInterval(interval int) int
fn C.SDL_GL_SwapWindow(window voidptr) fn C.SDL_GL_SwapWindow(window voidptr)
fn C.SDL_GL_DeleteContext(context voidptr) fn C.SDL_GL_DeleteContext(context voidptr)
pub fn create_texture_from_surface(renderer voidptr, surface &Surface) voidptr { pub fn create_texture_from_surface(renderer voidptr, surface &SDL_Surface) voidptr {
return C.SDL_CreateTextureFromSurface(renderer, voidptr(surface)) return C.SDL_CreateTextureFromSurface(renderer, voidptr(surface))
} }
@ -252,19 +213,19 @@ pub fn joystick_name_for_index(device_index int) byteptr {
return byteptr(C.SDL_JoystickNameForIndex(device_index)) return byteptr(C.SDL_JoystickNameForIndex(device_index))
} }
pub fn fill_rect(screen &Surface, rect &Rect, _col &Color) { pub fn fill_rect(screen &SDL_Surface, rect &SDL_Rect, _col &SDL_Color) {
col := C.SDL_MapRGB(screen.format, _col.r, _col.g, _col.b) col := C.SDL_MapRGB(screen.format, _col.r, _col.g, _col.b)
_screen := voidptr(screen) _screen := voidptr(screen)
_rect := voidptr(rect) _rect := voidptr(rect)
C.SDL_FillRect(_screen, _rect, col) C.SDL_FillRect(_screen, _rect, col)
} }
pub fn create_rgb_surface(flags u32, width int, height int, depth int, rmask u32, gmask u32, bmask u32, amask u32) &Surface { pub fn create_rgb_surface(flags u32, width int, height int, depth int, rmask u32, gmask u32, bmask u32, amask u32) &SDL_Surface {
res := C.SDL_CreateRGBSurface(flags, width, height, depth, rmask, gmask, bmask, amask) res := C.SDL_CreateRGBSurface(flags, width, height, depth, rmask, gmask, bmask, amask)
return res return res
} }
pub fn render_copy(renderer voidptr, texture voidptr, srcrect &Rect, dstrect &Rect) int { pub fn render_copy(renderer voidptr, texture voidptr, srcrect &SDL_Rect, dstrect &SDL_Rect) int {
_srcrect := voidptr(srcrect) _srcrect := voidptr(srcrect)
_dstrect := voidptr(dstrect) _dstrect := voidptr(dstrect)
return C.SDL_RenderCopy(renderer, texture, _srcrect, _dstrect) return C.SDL_RenderCopy(renderer, texture, _srcrect, _dstrect)
@ -278,7 +239,7 @@ pub fn destroy_texture(text voidptr) {
C.SDL_DestroyTexture(text) C.SDL_DestroyTexture(text)
} }
pub fn free_surface(surf &Surface) { pub fn free_surface(surf &SDL_Surface) {
_surf := voidptr(surf) _surf := voidptr(surf)
C.SDL_FreeSurface(_surf) C.SDL_FreeSurface(_surf)
} }

135
vlib/sdl/ttf/ttf.v Normal file
View File

@ -0,0 +1,135 @@
module ttf
#include <SDL_ttf.h>
[typedef]
struct C.TTF_Font {}
fn C.TTF_Init() int
fn C.TTF_Quit()
fn C.TTF_OpenFont(file byteptr, ptsize int) &TTF_Font
fn C.TTF_OpenFontIndex(file byteptr, ptsize int, index i64) &TTF_Font
fn C.TTF_OpenFontRW(src &SDL_RWops, freesrc int, ptsize int) &TTF_Font
fn C.TTF_OpenFontIndexRW(src &SDL_RWops, freesrc int, ptsize int, index i64) &TTF_Font
/* Set and retrieve the font style */
const (
TTF_STYLE_NORMAL = C.TTF_STYLE_NORMAL
TTF_STYLE_BOLD = C.TTF_STYLE_BOLD
TTF_STYLE_ITALIC = C.TTF_STYLE_ITALIC
TTF_STYLE_UNDERLINE = C.TTF_STYLE_UNDERLINE
TTF_STYLE_STRIKETHROUGH = C.TTF_STYLE_STRIKETHROUGH
)
fn C.TTF_GetFontStyle(font &TTF_Font) int
fn C.TTF_SetFontStyle(font &TTF_Font, style int)
fn C.TTF_GetFontOutline(font &TTF_Font) int
fn C.TTF_SetFontOutline(font &TTF_Font, outline int)
/* Set and retrieve FreeType hinter settings */
const (
TTF_HINTING_NORMAL = C.TTF_HINTING_NORMAL
TTF_HINTING_LIGHT = C.TTF_HINTING_LIGHT
TTF_HINTING_MONO = C.TTF_HINTING_MONO
TTF_HINTING_NONE = C.TTF_HINTING_NONE
)
fn C.TTF_GetFontHinting(font &TTF_Font) int
fn C.TTF_SetFontHinting(font &TTF_Font, hinting int)
/* Get the total height of the font - usually equal to point size */
fn C.TTF_FontHeight(font &TTF_Font) int
/* Get the offset from the baseline to the top of the font This is a positive value, relative to the baseline.
*/
fn C.TTF_FontAscent(font &TTF_Font) int
/* Get the offset from the baseline to the bottom of the font This is a negative value, relative to the baseline. */
fn C.TTF_FontDescent(font &TTF_Font) int
/* Get the recommended spacing between lines of text for this font */
fn C.TTF_FontLineSkip(font &TTF_Font) int
/* Get/Set whether or not kerning is allowed for this font */
fn C.TTF_GetFontKerning(font &TTF_Font) int
fn C.TTF_SetFontKerning(font &TTF_Font, allowed int)
/* Get the kerning size of two glyphs */
fn C.TTF_GetFontKerningSizeGlyphs(font &TTF_Font, previous_ch u16, ch u16) int
/* Get the number of faces of the font */
fn C.TTF_FontFaces(font &TTF_Font) i64
/* Get the font face attributes, if any */
fn C.TTF_FontFaceIsFixedWidth(font &TTF_Font) int
fn C.TTF_FontFaceFamilyName(font &TTF_Font) byteptr
fn C.TTF_FontFaceStyleName(font &TTF_Font) byteptr
/* Check wether a glyph is provided by the font or not */
fn C.TTF_GlyphIsProvided(font &TTF_Font, ch u16) int
/* Get the metrics (dimensions) of a glyph To understand what these metrics mean, here is a useful link:
http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
*/
fn C.TTF_GlyphMetrics(font &TTF_Font, ch u16, minx &int, maxx &int, miny &int, maxy &int, advance &int) int
/* Get the dimensions of a rendered string of text */
fn C.TTF_SizeText(font &TTF_Font, text byteptr, w &int, h &int) int
fn C.TTF_SizeUTF8(font &TTF_Font, text byteptr, w &int, h &int) int
fn C.TTF_SizeUNICODE(font &TTF_Font, text &u16, w &int, h &int) int
/* Create an 8-bit palettized surface and render the given text at fast quality with the given font and color. The 0 pixel is the
colorkey, giving a transparent background, and the 1 pixel is set to the text color.
This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderText_Solid(font &TTF_Font, text byteptr, fg SDL_Color) &SDL_Surface
fn C.TTF_RenderUTF8_Solid(font &TTF_Font, text byteptr, fg SDL_Color) &SDL_Surface
fn C.TTF_RenderUNICODE_Solid(font &TTF_Font, text &u16, fg SDL_Color) &SDL_Surface
/* Create an 8-bit palettized surface and render the given glyph at fast quality with the given font and color. The 0 pixel is the
colorkey, giving a transparent background, and the 1 pixel is set to the text color. The glyph is rendered without any padding or
centering in the X direction, and aligned normally in the Y direction. This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderGlyph_Solid(font &TTF_Font, ch u16, fg C.SDL_Color) &SDL_Surface
/* Create an 8-bit palettized surface and render the given text at high quality with the given font and colors. The 0 pixel is background,
while other pixels have varying degrees of the foreground color. This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderText_Shaded(font &TTF_Font, text byteptr, fg SDL_Color, bg SDL_Color) &SDL_Surface
fn C.TTF_RenderUTF8_Shaded(font &TTF_Font, text byteptr, fg SDL_Color, bg SDL_Color) &SDL_Surface
fn C.TTF_RenderUNICODE_Shaded(font &TTF_Font, text &u16, fg SDL_Color, bg SDL_Color) &SDL_Surface
/* Create an 8-bit palettized surface and render the given glyph at high quality with the given font and colors. The 0 pixel is background,
while other pixels have varying degrees of the foreground color. The glyph is rendered without any padding or centering in the X
direction, and aligned normally in the Y direction. This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderGlyph_Shaded(font &TTF_Font, ch u16, fg C.SDL_Color) &SDL_Surface
/* Create a 32-bit ARGB surface and render the given text at high quality, using alpha blending to dither the font with the given color.
This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderText_Blended(font &TTF_Font, text byteptr, fg SDL_Color, bg SDL_Color) &SDL_Surface
fn C.TTF_RenderUTF8_Blended(font &TTF_Font, text byteptr, fg SDL_Color, bg SDL_Color) &SDL_Surface
fn C.TTF_RenderUNICODE_Blended(font &TTF_Font, text &u16, fg SDL_Color, bg SDL_Color) &SDL_Surface
/* Create a 32-bit ARGB surface and render the given text at high quality, using alpha blending to dither the font with the given color.
Text is wrapped to multiple lines on line endings and on word boundaries if it extends beyond wrapLength in pixels.
This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderText_Blended_Wrapped(font &TTF_Font, text byteptr, fg SDL_Color, wrap_length u32) &SDL_Surface
fn C.TTF_RenderUTF8_Blended_Wrapped(font &TTF_Font, text byteptr, fg SDL_Color, wrap_length u32) &SDL_Surface
fn C.TTF_RenderUNICODE_Blended_Wrapped(font &TTF_Font, text &u16, fg SDL_Color, wrap_length u32) &SDL_Surface
/* Create a 32-bit ARGB surface and render the given glyph at high quality, using alpha blending to dither the font with the given color.
The glyph is rendered without any padding or centering in the X direction, and aligned normally in the Y direction.
This function returns the new surface, or NULL if there was an error.
*/
fn C.TTF_RenderGlyph_Blended(font &TTF_Font, ch u16, fg C.SDL_Color) &SDL_Surface
fn C.TTF_WasInit() int
fn C.TTF_CloseFont(font &TTF_Font)
pub const (
version = '0.0.1'
)