Claudio Ciccani | fe94d0b | 2006-05-31 17:05:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | (c) Copyright 2001 convergence integrated media GmbH. |
| 3 | All rights reserved. |
| 4 | |
| 5 | Written by Denis Oliver Kropp <dok@convergence.de> and |
| 6 | Andreas Hundt <andi@convergence.de>. |
| 7 | |
| 8 | This library is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2 of the License, or (at your option) any later version. |
| 12 | |
| 13 | This library is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this library; if not, write to the |
| 20 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | Boston, MA 02111-1307, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <math.h> |
| 28 | |
| 29 | #include <GL/gl.h> |
| 30 | #include <GL/glu.h> |
| 31 | |
| 32 | #include <directfb.h> |
| 33 | #include <directfbgl.h> |
| 34 | |
| 35 | |
| 36 | typedef struct { |
| 37 | IDirectFBWindow *window; |
| 38 | IDirectFBSurface *surface; |
| 39 | IDirectFBGL *gl; |
| 40 | |
| 41 | int width; |
| 42 | int height; |
| 43 | |
| 44 | unsigned long last_time; |
| 45 | int frames; |
| 46 | float fps; |
| 47 | } Context; |
| 48 | |
| 49 | static const GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0}; |
| 50 | |
| 51 | static IDirectFB *dfb; |
| 52 | static IDirectFBDisplayLayer *layer; |
| 53 | static IDirectFBFont *font; |
| 54 | static IDirectFBEventBuffer *events = NULL; |
| 55 | |
| 56 | /* macro for a safe call to DirectFB functions */ |
| 57 | #define DFBCHECK(x...) \ |
| 58 | do { \ |
| 59 | ret = x; \ |
| 60 | if (ret != DFB_OK) { \ |
| 61 | fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \ |
| 62 | DirectFBErrorFatal( #x, ret ); \ |
| 63 | } \ |
| 64 | } while (0) |
| 65 | |
| 66 | |
| 67 | static inline unsigned long get_millis() |
| 68 | { |
| 69 | struct timeval tv; |
| 70 | |
| 71 | gettimeofday (&tv, NULL); |
| 72 | return (tv.tv_sec * 1000 + tv.tv_usec / 1000); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static void |
| 77 | setup( Context *context ) |
| 78 | { |
| 79 | GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0}; |
| 80 | |
| 81 | context->surface->GetSize( context->surface, |
| 82 | &context->width, &context->height ); |
| 83 | |
| 84 | context->gl->Lock( context->gl ); |
| 85 | |
| 86 | glLightfv(GL_LIGHT0, GL_POSITION, pos); |
| 87 | glEnable(GL_CULL_FACE); |
| 88 | glEnable(GL_LIGHTING); |
| 89 | glEnable(GL_LIGHT0); |
| 90 | glEnable(GL_DEPTH_TEST); |
| 91 | |
| 92 | glViewport(0, 0, context->width, context->height); |
| 93 | |
| 94 | glMatrixMode(GL_PROJECTION); |
| 95 | glLoadIdentity(); |
| 96 | gluPerspective(70.0, context->width / (float) context->height, 1.0, 80.0); |
| 97 | |
| 98 | glMatrixMode(GL_MODELVIEW); |
| 99 | glLoadIdentity(); |
| 100 | glTranslatef(0.0, 0.0, -40.0); |
| 101 | |
| 102 | context->gl->Unlock( context->gl ); |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | update( Context *context ) |
| 107 | { |
| 108 | unsigned long t; |
| 109 | IDirectFBSurface *surface = context->surface; |
| 110 | static __u8 r = 0, g = 0, b = 0; |
| 111 | |
| 112 | |
| 113 | context->gl->Lock( context->gl ); |
| 114 | |
| 115 | glClearColor( r++/255.0, g++/255.0, b++/255.0, 1.0 ); |
| 116 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 117 | |
| 118 | context->gl->Unlock( context->gl ); |
| 119 | |
| 120 | if (context->fps) { |
| 121 | char buf[16]; |
| 122 | |
| 123 | snprintf(buf, sizeof(buf), "%.1f FPS\n", context->fps); |
| 124 | |
| 125 | surface->SetColor( surface, 0xff, 0x00, 0x00, 0xff ); |
| 126 | surface->DrawString( surface, buf, -1, |
| 127 | context->width - 5, 5, DSTF_TOPRIGHT ); |
| 128 | } |
| 129 | |
| 130 | surface->Flip( surface, NULL, 0 ); |
| 131 | |
| 132 | context->frames++; |
| 133 | |
| 134 | t = get_millis(); |
| 135 | if (t - context->last_time >= 2000) { |
| 136 | float seconds = (t - context->last_time) / 1000.0f; |
| 137 | |
| 138 | context->fps = context->frames / seconds; |
| 139 | |
| 140 | context->last_time = t; |
| 141 | context->frames = 0; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | main( int argc, char *argv[] ) |
| 147 | { |
| 148 | DFBResult ret; |
| 149 | int i; |
| 150 | int quit = 0; |
| 151 | const int num = 2; |
| 152 | Context contexts[num]; |
| 153 | |
| 154 | DFBCHECK(DirectFBInit( &argc, &argv )); |
| 155 | |
| 156 | /* create the super interface */ |
| 157 | DFBCHECK(DirectFBCreate( &dfb )); |
| 158 | |
| 159 | DFBCHECK(dfb->GetDisplayLayer( dfb, DLID_PRIMARY, &layer )); |
| 160 | |
| 161 | /* create the default font */ |
| 162 | DFBCHECK(dfb->CreateFont( dfb, NULL, NULL, &font )); |
| 163 | |
| 164 | for (i=0; i<num; i++) { |
| 165 | IDirectFBWindow *window; |
| 166 | IDirectFBSurface *surface; |
| 167 | IDirectFBGL *gl; |
| 168 | DFBWindowDescription desc; |
| 169 | |
| 170 | desc.flags = DWDESC_POSX | DWDESC_POSY | |
| 171 | DWDESC_WIDTH | DWDESC_HEIGHT; |
| 172 | desc.posx = (i%3) * 200 + 10; |
| 173 | desc.posy = (i/3) * 200 + 10; |
| 174 | desc.width = 180; |
| 175 | desc.height = 180; |
| 176 | |
| 177 | DFBCHECK(layer->CreateWindow( layer, &desc, &window )); |
| 178 | DFBCHECK(window->GetSurface( window, &surface )); |
| 179 | DFBCHECK(surface->GetGL( surface, &gl )); |
| 180 | |
| 181 | contexts[i].window = window; |
| 182 | contexts[i].surface = surface; |
| 183 | contexts[i].gl = gl; |
| 184 | |
| 185 | contexts[i].last_time = get_millis(); |
| 186 | contexts[i].frames = 0; |
| 187 | contexts[i].fps = 0; |
| 188 | |
| 189 | setup( &contexts[i] ); |
| 190 | |
| 191 | if (events) |
| 192 | DFBCHECK(window->AttachEventBuffer( window, events )); |
| 193 | else |
| 194 | DFBCHECK(window->CreateEventBuffer( window, &events )); |
| 195 | |
| 196 | DFBCHECK(surface->SetFont( surface, font )); |
| 197 | |
| 198 | window->SetOpacity( window, 0xff ); |
| 199 | } |
| 200 | |
| 201 | while (!quit) { |
| 202 | DFBWindowEvent evt; |
| 203 | |
| 204 | for (i=0; i<num; i++) |
| 205 | update( &contexts[i] ); |
| 206 | |
| 207 | while (events->GetEvent( events, DFB_EVENT(&evt) ) == DFB_OK) { |
| 208 | switch (evt.type) { |
| 209 | case DWET_KEYDOWN: |
| 210 | switch (evt.key_symbol) { |
| 211 | case DIKS_ESCAPE: |
| 212 | quit = 1; |
| 213 | break; |
| 214 | |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | events->Release( events ); |
| 227 | |
| 228 | for (i=0; i<num; i++) { |
| 229 | contexts[i].gl->Release( contexts[i].gl ); |
| 230 | contexts[i].surface->Release( contexts[i].surface ); |
| 231 | contexts[i].window->Release( contexts[i].window ); |
| 232 | } |
| 233 | |
| 234 | font->Release( font ); |
| 235 | layer->Release( layer ); |
| 236 | dfb->Release( dfb ); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |