annotate R/rdyncall/demo/sdlnet.R @ 3:f5d4d5c2f750

- gemspec fix
author cslag
date Thu, 31 Dec 2015 20:24:53 +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 # BUGS: does not work on Mac OS X using R64, needs to use R32.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 library(rdyncall)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 dynport(SDL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 dynport(GL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 dynport(SDL_net)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 library(mapdata)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 db <- "worldHires"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 PORT <- 1234
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 MAX_CLIENT_SOCKETS <- 3
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 tcp <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 socket <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 sockets <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 ctcps <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 csocks <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 world <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 glList <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 init <- function() {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 if ( SDL_Init(SDL_INIT_VIDEO) == -1 ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 error("SDL_Init failed")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 if ( SDLNet_Init() == -1 ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 error("SDLNet_Init failed")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 ip <- new.struct("IPaddress")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 if ( SDLNet_ResolveHost(ip,NULL,PORT) == -1 ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 error("SDLNet_ResolveHost failed")
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 tcp <<- SDLNet_TCP_Open(ip)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 socket <<- as.struct(offsetPtr(tcp,0),"SDLNet_GenericSocket_")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 sockets <<- SDLNet_AllocSocketSet(1+MAX_CLIENT_SOCKETS)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 SDLNet_AddSocket(sockets, socket )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 SDL_SetVideoMode(640,480,32,SDL_OPENGL+SDL_DOUBLEBUF)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 ctcps <<- list()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 csocks <<- list()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 world <<- map(db,"switzerland",plot=FALSE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 glList <<- glGenLists(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 drawMap3d <- function(m) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 glNewList(glList, GL_COMPILE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 x <- m$x
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 vb <- rbind(m$x,m$y)
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 glVertexPointer(2, GL_DOUBLE, 0, vb)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 markers <- which(is.na(x))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 begin <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 i <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 while(i <= length(markers)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 end <- markers[i] - 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 glDrawArrays(GL_LINE_STRIP, begin - 1, (end-1) - (begin-1) + 1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 begin <- markers[i] + 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 i <- i + 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 end <- length(x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 glDrawArrays(GL_LINE_STRIP, begin - 1, (end-1) - (begin-1) + 1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 glDisableClientState(GL_VERTEX_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 glEndList()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 loop <- function() {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 drawMap3d(world)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 do_loop <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 cnt <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 evt <- new.struct("SDL_Event")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 while(do_loop) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 glClearColor(0.2,0.3,0.1,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 glClear(GL_COLOR_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 r <- world$range
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 glMatrixMode(GL_PROJECTION)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 glOrtho(r[[1]],r[[2]],r[[3]],r[[4]],-10000, 10000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 glMatrixMode(GL_MODELVIEW)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 cx <- r[[1]] + ( r[[2]] - r[[1]] ) * 0.5
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 cy <- r[[3]] + ( r[[4]] - r[[3]] ) * 0.5
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 glTranslatef( cx, cy , 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 glRotatef(cnt,0,1,0) ; cnt <- cnt + 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 glTranslatef( -cx, -cy , 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 #( r[[2]]-r[[1]] )*0.5, (r[[4]]-r[[3]]) *0.5,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 # glTranslatef(-(r[[2]]-r[[1]])*0.5,-(r[[4]]-r[[3]])*0.5,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 glEnable(GL_BLEND)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 glColor4f(1.0,0.8,0.5,0.2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102 delta <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 for(i in 1:10) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 glTranslatef( cx, cy , 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106 glRotatef(cnt+delta,0,1,0) ;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 glTranslatef( -cx, -cy , 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 glCallList(glList)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 delta <- delta + 1.0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 glFinish()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 while( SDL_PollEvent(evt) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 if ( evt$type == SDL_QUIT ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 do_loop <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 numready <- SDLNet_CheckSockets(sockets, 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 if (numready > 0) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 cat("ready\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 if (socket$ready) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 cat("listener\n\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 # tcp <- as.struct(tcp,"_TCPsocket")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 ctcp <- SDLNet_TCP_Accept(tcp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 if (is.null(ctcp)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 cat("warning: client is NULL\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 csock <- as.struct(offsetPtr(ctcp,0),"SDLNet_GenericSocket_")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 ctcps <- c(ctcps, ctcp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 csocks <- c(csocks, csock)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 if ( SDLNet_AddSocket(sockets, csock) == -1 ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 cat("warning: add socket failed\n")
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 numready <- numready - 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 while(numready) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 i <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 for(i in 1:length(csocks)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 csock <- csocks[[i]]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 if(csock$ready) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 cat("client ready")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 numready <- numready - 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 buf <- raw(1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 result <- SDLNet_TCP_Recv(ctcps[[i]], buf, length(buf))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 if (result <= 0) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 cat("ERROR: SDLNet_TCP_Recv result <= 0.\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 buf[result] <- as.raw(0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 txt <- ptr2str(offsetPtr(buf,0))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 cat("DATA:'",txt,"'\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 tryCatch({
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 m <- map(db,txt,plot=FALSE)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 world <<- m
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158 drawMap3d(world)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 },error= function(x) {})
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 SDL_Delay(20)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 loop()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170