annotate R/rdyncall/demo/ttf.R @ 30:baf087cf5971

- fixed two ref counting problems - minor improvements
author Tassilo Philipp
date Fri, 10 Apr 2020 20:35:19 +0200
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/ttf.R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 # Description: TrueType Font loading and drawing via SDL and SDL_ttf.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 dynport(SDL_ttf)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 fbSurf <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 textSurf <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 numTexts <- 10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 init <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 status <- TTF_Init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 if (status != 0) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 stop(paste("TTF_Init failed: ", TTF_GetError(), sep=""))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 # tryPaths <- c("/Library/Fonts","/usr/X11R7","/usr/X11R6")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 # tryFonts <- c("Sathu.ttf", "Vera.ttf")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 font <- TTF_OpenFont("/usr/X11R7/lib/X11/fonts/TTF/Vera.ttf",48)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 # Library/Fonts/Sathu.ttf",48)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 if (is.nullptr(font)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 stop(paste("TTF_OpenFont failed: ", TTF_GetError(), sep=""))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 color <- new.struct(SDL_Color)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 color$r <- color$g <- color$b <- 255
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 textSurf <<- TTF_RenderText_Solid(font, "Hello World.")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 SDL_Init(SDL_INIT_VIDEO)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 fbSurf <<- SDL_SetVideoMode(256,256,32,SDL_DOUBLEBUF)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 displace <<- rnorm(numTexts*2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 main <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 rect <- new.struct(SDL_Rect)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 rect$x <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 rect$y <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 rect$w <- textSurf$w
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 rect$h <- textSurf$h
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 rect2 <- rect
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 evt <- new.struct(SDL_Event)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 distance <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 while(!quit) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 SDL_FillRect(fbSurf, as.struct( as.extptr(NULL), "SDL_Rect" ), 0xFFFFFFL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 rect
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 i <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 while(i < numTexts*2) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 rect2$x <- rect$x + distance * displace[i]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 rect2$y <- rect$y + distance * displace[i+1]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 i <- i + 2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 SDL_BlitSurface(textSurf, as.struct(as.extptr(NULL),"SDL_Rect"),fbSurf,rect2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 SDL_Flip(fbSurf)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 distance <- distance + 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 while ( SDL_PollEvent(evt) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 if ( evt$type == SDL_QUIT )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 quit <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 else if (evt$type == SDL_MOUSEBUTTONDOWN ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 rect$x <- evt$button$x
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 rect$y <- evt$button$y
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 distance <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 run <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 main()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92