annotate R/rdyncall/demo/randomfield2.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/randomfield.R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 # Description: Scientific Computations using OpenGL: Rendering 512x512 random field by blending 5000 point sprites (dynport demo)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 require(rdyncall)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 library(lattice)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 dynport(SDL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 dynport(GL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 # ----------------------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 # Parameters:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 # Framebuffer size
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 fb.size <- 512
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 # Texture size
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 tex.size <- 512
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 # Blending color
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 colorunit <- 0.02
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 # ----------------------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 # texture setup
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 genTex.circle <- function(n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 m <- matrix(data=FALSE,nr=n,nc=n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 r <- n/2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 for(i in 1:n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 for(j in 1:n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 m[[i,j]] <- ifelse((i-r)^2+(j-r)^2 > r^2,0L,255L)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 return(m)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 genTex.bnorm <- function(n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 x <- seq(-3,3,len=n)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 y <- dnorm(x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 return( outer(y,y) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 # ----------------------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 # draw circle using OpenGL Vertex Arrays
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 drawTexCirclesVertexArray <- function(x,y,r)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 n <- max( length(x), length(y), length(r) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 x1 <- x-r
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 x2 <- x+r
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 y1 <- y-r
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 y2 <- y+r
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 vertexArray <- as.vector(rbind(x1,y1,x2, y1, x2, y2, x1, y2))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 texCoordArray <- rep( as.double(c(0,0,1,0,1,1,0,1)), n )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 glEnableClientState(GL_VERTEX_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 glVertexPointer(2,GL_DOUBLE,0,vertexArray)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 glTexCoordPointer(2,GL_DOUBLE,0,texCoordArray)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 glDrawArrays(GL_QUADS, 0, n*4)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 glDisableClientState(GL_VERTEX_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 glDisableClientState(GL_TEXTURE_COORD_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 #drawPointSprite <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 #{
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 # glEnable(GL_POINT_SPRITE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 # glTexEnvi(GL_POINT_SPRITE,GL_COORD_REPLACE,GL_TRUE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 # glPointParameter(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 # glPointSize
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 #}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 # ----------------------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 # initialize SDL, OpenGL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 max.tex.size <- integer(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 max.tex.units <- integer(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 tex.ids <- integer(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 init <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 # initialize SDL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 SDL_Init(SDL_INIT_VIDEO)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 surface <<- SDL_SetVideoMode(fb.size,fb.size,32,SDL_OPENGL+SDL_DOUBLEBUF)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 # initialize OpenGL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 glGetIntegerv(GL_MAX_TEXTURE_SIZE, max.tex.size)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 glGetIntegerv(GL_MAX_TEXTURE_UNITS, max.tex.units)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 glClearColor(0,0,0,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 glColor4f(0.1,0.1,0.1,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 # img <- genTex.circle(tex.size)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 # texdata <- as.raw(img)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 img <- genTex.bnorm(tex.size)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 m <- max(img)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 texdata <- as.raw( ( img/m ) * 255 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 glGenTextures( length(tex.ids),tex.ids)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 glBindTexture(GL_TEXTURE_2D, tex.ids[[1]])
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex.size, tex.size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texdata)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 # glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex.size, tex.size, 0, GL_ALPHA, GL_DOUBLE, as.vector(img))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 glEnable(GL_TEXTURE_2D)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 # blending setup
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 glBlendFunc(GL_SRC_ALPHA, GL_ONE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 glEnable(GL_BLEND)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 glColor3d( colorunit,colorunit,colorunit )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 cleanup <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 glDeleteTextures( length(tex.ids), tex.ids)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 SDL_Quit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 sim <- function(N = 5000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 return(list(
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 x = runif(N,-1.1,+1.1),
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 y = runif(N,-1.1,+1.1),
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 r = runif(N, 0.1, 0.2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 ))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 render <- function(o) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 glClear(GL_COLOR_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 drawTexCirclesVertexArray(o$x,o$y,o$r)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 glFinish()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 event <- new.struct("SDL_Event")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 pollEvents <- function() {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154 while( SDL_PollEvent(event) != 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 type <- event$type
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 if (type == SDL_MOUSEBUTTONDOWN) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158 cat("Read pixels via OpenGL into an R integer matrix..")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 pixels <<- readpixels()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 cat("done.\nPlot image results with R plotting device. This may take a while - please be patient..")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 image(pixels)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 cat("done.\nContinue..\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 } else if (type == SDL_QUIT) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 cat("Read pixels via OpenGL into an R integer matrix 'pixels'..")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 pixels <<- readpixels()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 cat("done.\nRe-run by 'run()'\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 quit <<- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172 # --- Read Pixels ------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 readpixels <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 array <- matrix(NA_integer_,fb.size,fb.size)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 glPixelStorei(GL_PACK_ALIGNMENT,1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 glReadPixels(0,0,fb.size,fb.size, GL_LUMINANCE, GL_INT, array)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 return(array)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 # --- FPS Counter ------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 fpsInit <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 return(list(
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 tbase = SDL_GetTicks(),
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 frames = 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 ))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 fpsUpdate <- function(o) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 tnow <- SDL_GetTicks()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 if ((tnow - o$tbase) > 1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 o$tbase <- tnow
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
196 SDL_WM_SetCaption(paste("FPS:", o$frames),NULL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197 o$frames <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
198 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199 o$frames <- o$frames + 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 return(o)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 # --- Main Loop --------------------------------------------------------------
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 main <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
207 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 # disable interactive plot device.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210 oldpars <- par(ask=FALSE,mfrow=c(1,1))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211 # user notice
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 cat("Click on window to import current random field and plot in R.\nClose window to quit mainloop.\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
213
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
214 fps <- fpsInit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
215 quit <<- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 while(!quit)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 sim <- sim(5000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 render(sim)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
220 pollEvents()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 fps <- fpsUpdate(fps)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 par(oldpars)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
226
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
227 main()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
228