annotate R/rdyncall/demo/intro.R @ 58:e4bf6e44fbf5

fixes to test code
author Tassilo Philipp
date Mon, 08 Feb 2021 10:15:27 +0100
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 # Package: rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 # File: demo/intro.R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 # Description: Texture-mapped scroll-text, playing music 'Hybrid Song' composed in jan. -96 by Quazar of Sanxion
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 s <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 texId <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 music <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 checkGL <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 glerror <- glGetError()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 if (glerror != 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 cat("GL Error", glerror, "\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 return(glerror == 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 init <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 require(rdyncall)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 dynport(SDL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 SDL_Init(SDL_INIT_VIDEO+SDL_INIT_AUDIO)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 dynport(GL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 dynport(SDL_image)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 s <<- SDL_SetVideoMode(640,480,32,SDL_OPENGL+SDL_DOUBLEBUF)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 stopifnot( IMG_Init(IMG_INIT_PNG) == IMG_INIT_PNG )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 texId <<- loadTexture("chromefont.png")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 # texId <<- loadTexture("nuskool_krome_64x64.png")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 dynport(SDL_mixer)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 # stopifnot( Mix_Init(MIX_INIT_MOD) == MIX_INIT_MOD )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 music <<- Mix_LoadMUS(rsrc("external.xm"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 rsrc <- function(name) system.file(paste("demo-files",name,sep=.Platform$file.sep), package="rdyncall")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 loadTexture <- function(name)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 checkGL()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 glEnable(GL_TEXTURE_2D)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 x <- rsrc(name)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 img <- IMG_Load(x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 # glPixelStorei(GL_UNPACK_ALIGNMENT,4)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 texid <- integer(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 glGenTextures(1, texid)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 glBindTexture(GL_TEXTURE_2D, texid)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 SDL_LockSurface(img)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 maxS <- integer(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxS)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 stopifnot( (img$w <= maxS) && (img$h <= maxS) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 glTexImage2D(GL_TEXTURE_2D, 0, 4, img$w, img$h, 0, GL_BGRA, GL_UNSIGNED_BYTE, img$pixels)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 SDL_UnlockSurface(img)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 SDL_FreeSurface(img)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 # gluBuild2DMipmaps(GL_TEXTURE_2D, 4, img$w, img$h)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 return(texid)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 drawScroller <- function(codes,time)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 glBindTexture(GL_TEXTURE_2D, texId)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 glEnable(GL_BLEND)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 glMatrixMode(GL_MODELVIEW)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 glMatrixMode(GL_PROJECTION)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 x <- 1-time*0.5
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 y <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 w <- 0.3+0.1*sin(6.24*time)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 h <- 0.2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 for (i in 1:length(codes)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 t <- codes[i]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 s0 <- (t%%8)/8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 t0 <- as.integer(t/8)/8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 s1 <- s0+1/8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 t1 <- t0+1/8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 # s0 <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 # s1 <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 # t0 <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 # t1 <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 glBegin(GL_QUADS)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 glTexCoord2f(s0,t1) ; glVertex3f(x ,y ,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 glTexCoord2f(s1,t1) ; glVertex3f(x+w,y ,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 glTexCoord2f(s1,t0) ; glVertex3f(x+w,y+h,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 glTexCoord2f(s0,t0) ; glVertex3f(x ,y+h,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 glEnd()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 x <- x + w
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 codes <- utf8ToInt("DO YOU SOMETIMES WANT FOR YOUR OLD HOME COMPUTER?! - I DO") - 32
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 mainloop <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 Mix_PlayMusic(music, 1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 blink <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 tbase <- SDL_GetTicks()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 evt <- new.struct(SDL_Event)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 while(!quit)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 tnow <- SDL_GetTicks()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 tdemo <- ( tnow - tbase ) / 1000
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 glClearColor(0,0,blink,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 blink <- blink + 0.01
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 drawScroller(codes,tdemo)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 while( SDL_PollEvent(evt) != 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 type <- evt$type
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 if (
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 type == SDL_QUIT
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 || ( type == SDL_KEYDOWN && evt$key$keysym$sym == SDLK_ESCAPE )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 quit <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 SDL_Delay(20)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 cleanup <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 Mix_CloseAudio()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136 # Mix_Quit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 IMG_Quit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 SDL_Quit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 run <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 mainloop()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 cleanup()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 run()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149