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

@ -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 sdl
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 (
Title = 'tVintris'
@ -68,6 +70,11 @@ const (
JHP2RIGHT = 2
)
const (
mix_version = mix.version
ttf_version = ttf.version
)
const (
// Tetros' 4 possible states are encoded in binaries
BTetros = [
@ -107,25 +114,22 @@ const (
]
// Each tetro has its unique color
Colors = [
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(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(0), byte(0)}, // short topleft : lightgreen 00c100
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(0), byte(0)}, // longest : lightred d10000
sdl.Color{byte(0), byte(170), byte(170), 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(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(0), byte(0)}, // short topleft : lightgreen 00c100
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(0), byte(0)}, // longest : lightred d10000
SDL_Color{byte(0), byte(170), byte(170), byte(0)}, // unused ?
]
// Background color
BackgroundColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)}
// BackgroundColor = sdl.Color{byte(255), byte(255), byte(255), byte(0)}
BackgroundColor = SDL_Color{byte(0), byte(0), byte(0), byte(0)}
// Foreground color
ForegroundColor = sdl.Color{byte(0), byte(170), byte(170), byte(0)}
// ForegroundColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)}
ForegroundColor = SDL_Color{byte(0), byte(170), byte(170), byte(0)}
// Text color
TextColor = sdl.Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}
// TextColor = sdl.Color{byte(0), byte(0), byte(0), byte(0)}
TextColor = SDL_Color{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}
)
// TODO: type Tetro [TetroSize]struct{ x, y int }
@ -154,7 +158,7 @@ mut:
h int
window voidptr
renderer voidptr
screen &sdl.Surface
screen &SDL_Surface
texture voidptr
// AUDIO
actx AudioContext
@ -162,7 +166,7 @@ mut:
jnames [2]string
jids [2]int
// V logo
v_logo &sdl.Surface
v_logo &SDL_Surface
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.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)
if C.Mix_OpenAudio(48000,C.MIX_DEFAULT_FORMAT,2,AudioBufSize) < 0 {
println('couldn\'t open audio')
@ -375,7 +379,7 @@ fn main() {
match int(ev._type) {
C.SDL_QUIT { should_close = true }
C.SDL_KEYDOWN {
key := int(ev.key.keysym.sym)
key := ev.key.keysym.sym
if key == C.SDLK_ESCAPE {
should_close = true
break
@ -385,7 +389,7 @@ fn main() {
}
C.SDL_JOYBUTTONDOWN {
jb := int(ev.jbutton.button)
joyid := int(ev.jbutton.which)
joyid := ev.jbutton.which
// println('JOY BUTTON $jb $joyid')
game.handle_jbutton(jb, joyid)
game2.handle_jbutton(jb, joyid)
@ -393,7 +397,7 @@ fn main() {
C.SDL_JOYHATMOTION {
jh := int(ev.jhat.hat)
jv := int(ev.jhat.value)
joyid := int(ev.jhat.which)
joyid := ev.jhat.which
// println('JOY HAT $jh $jv $joyid')
game.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) {
// global keys
mut action := Action(.idle)
mut action := Action.idle
match key {
C.SDLK_SPACE { action = .space }
game.k_fire { action = .fire }
@ -488,7 +492,7 @@ fn (game mut Game) handle_jbutton(jb int, joyid int) {
return
}
// global buttons
mut action := Action(.idle)
mut action := Action.idle
match jb {
game.jb_fire { action = .fire }
else {}
@ -708,7 +712,7 @@ fn (g &Game) draw_tetro() {
}
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}
col := Colors[color_idx]
sdl_fill_rect(g.sdl.screen, &rect, &col)
@ -732,27 +736,27 @@ fn (g &Game) draw_v_logo() {
texw := 0
texh := 0
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)
// sdl.render_copy(g.sdl.renderer, tv_logo, 0, &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}
tsurf := C.TTF_RenderText_Solid(g.font, text.str, _tcol)
ttext := C.SDL_CreateTextureFromSurface(g.sdl.renderer, tsurf)
texw := 0
texh := 0
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)
C.SDL_RenderCopy(g.sdl.renderer, ttext, voidptr(0), voidptr(&dstrect))
C.SDL_DestroyTexture(ttext)
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)
}
@ -760,14 +764,14 @@ fn (g &Game) draw_text(x int, y int, text string, tcol sdl.Color) {
fn (g &Game) draw_begin() {
// println('about to clear')
C.SDL_RenderClear(g.sdl.renderer)
mut rect := sdl.Rect {0,0,g.sdl.w,g.sdl.h}
col := sdl.Color{byte(00), byte(00), byte(0), byte(0)}
mut rect := SDL_Rect {0,0,g.sdl.w,g.sdl.h}
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, 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)
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)
mut idx := 0
@ -778,7 +782,7 @@ fn (g &Game) draw_begin() {
}
w := BlockSize
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])
idx++
}
@ -831,7 +835,7 @@ fn parse_binary_tetro(t_ int) []Block {
for i := 0; i <= 3; i++ {
// Get ith digit of t
p := int(math.pow(10, 3 - i))
mut digit := int(t / p)
mut digit := t / p
t %= p
// Convert the digit to binary
for j := 3; j >= 0; j-- {