# HG changeset patch # User Eli Gottlieb # Date 1275355386 14400 # Node ID cf7976fd325837971f8426f50a74a5529473da64 # Parent 83f9b95da263f9261ac36ab71e291688e4fef5cf Moved the SDL_Eyes test program for shaped windows into the Hg repository and got it building and linking as a test. diff -r 83f9b95da263 -r cf7976fd3258 Makefile.in --- a/Makefile.in Thu May 27 16:44:35 2010 -0400 +++ b/Makefile.in Mon May 31 21:23:06 2010 -0400 @@ -44,7 +44,7 @@ DIST = acinclude autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS include INSTALL Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-Win32.zip WhatsNew Xcode Xcode-iPhoneOS -HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h +HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_shape.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h LT_AGE = @LT_AGE@ LT_CURRENT = @LT_CURRENT@ diff -r 83f9b95da263 -r cf7976fd3258 test/Makefile.in --- a/test/Makefile.in Thu May 27 16:44:35 2010 -0400 +++ b/test/Makefile.in Mon May 31 21:23:06 2010 -0400 @@ -7,7 +7,7 @@ CFLAGS = @CFLAGS@ LIBS = @LIBS@ -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmmousetablet$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testeyes$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmmousetablet$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) all: Makefile $(TARGETS) @@ -62,6 +62,9 @@ testerror$(EXE): $(srcdir)/testerror.c $(CC) -o $@ $? $(CFLAGS) $(LIBS) +testeyes$(EXE): $(srcdir)/testeyes.c + $(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS) + testfile$(EXE): $(srcdir)/testfile.c $(CC) -o $@ $? $(CFLAGS) $(LIBS) diff -r 83f9b95da263 -r cf7976fd3258 test/README --- a/test/README Thu May 27 16:44:35 2010 -0400 +++ b/test/README Mon May 31 21:23:06 2010 -0400 @@ -12,6 +12,7 @@ testcursor Tests custom mouse cursor testdyngl Tests dynamically loading OpenGL library testerror Tests multi-threaded error handling + testeyes Tests shaped windows testfile Tests RWops layer testgamma Tests video device gamma ramp testgl A very simple example of using OpenGL with SDL diff -r 83f9b95da263 -r cf7976fd3258 test/testeyes Binary file test/testeyes has changed diff -r 83f9b95da263 -r cf7976fd3258 test/testeyes.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/testeyes.c Mon May 31 21:23:06 2010 -0400 @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include +#include +#include "testeyes_bitmap.h" +#include "testeyes_mask_bitmap.h" + +/* The following code for the calculation of pupil positions has been taken and rewritten from the original xeyes. The + copyright notice is included as follows, and the code is now under the same license as the rest of this file. */ +/* + +Copyright (c) 1991 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ +# define NUM_EYES 2 +# define EYE_X(n) ((n) * 2.0) +# define EYE_Y(n) (0.0) +# define EYE_THICK (0.175) /* thickness of eye rim */ +# define EYE_OFFSET (0.1) /* padding between eyes */ +# define EYE_WIDTH (2.0 - (EYE_THICK + EYE_OFFSET) * 2) +# define EYE_HEIGHT EYE_WIDTH +# define EYE_HWIDTH (EYE_WIDTH / 2.0) +# define EYE_HHEIGHT (EYE_HEIGHT / 2.0) +# define BALL_WIDTH (0.3) +# define BALL_HEIGHT BALL_WIDTH +# define BALL_PAD (0.05) +# define BALL_DIST ((EYE_WIDTH - BALL_WIDTH) / 2.0 - BALL_PAD) + +SDL_Point computePupil (int num,SDL_Point mouse) { + double dx = mouse.x - EYE_X(num); + double dy = mouse.y - EYE_Y(num); + double cx = 0.0,cy = 0.0; + if(dx == 0 && dy == 0) { + cx = EYE_X(num); + cy = EYE_Y(num); + } + else { + double angle = atan2((double)dy,(double)dx); + double cosa = cos(angle); + double sina = sin(angle); + double hypotenuse = hypot(EYE_HHEIGHT * cosa, EYE_HWIDTH * sina); + double dist = BALL_DIST * hypot((EYE_HWIDTH * EYE_HHEIGHT) * cosa / hypotenuse, + (EYE_HWIDTH * EYE_HHEIGHT) * sina / hypotenuse); + if(dist > hypot((double)dx,(double)dy)) { + cx = dx + EYE_X(num); + cy = dy + EYE_Y(num); + } + else { + cx = dist * cosa + EYE_X(num); + cy = dist * sina + EYE_Y(num); + } + } + SDL_Point ret = {cx,cy}; + return ret; +} + +/* Here begins the code exclusively and entirely written by Eli Gottlieb in May 2010. */ +typedef struct { + SDL_Rect left; + SDL_Rect right; +} Pupil_Pair; +typedef struct { + SDL_Point left; + SDL_Point right; +} Pupil_Points; + +Pupil_Pair compute_pupil_positions(SDL_Point target) { + Pupil_Pair result; + Pupil_Points points; + points.left = computePupil(0,target); + points.right = computePupil(1,target); + result.left.x = points.left.x - BALL_WIDTH / 2.0; + result.left.y = points.left.y - BALL_HEIGHT / 2.0; + result.right.x = points.right.x - BALL_WIDTH / 2.0; + result.right.y = points.right.y - BALL_HEIGHT / 2.0; + result.left.w = result.right.w = BALL_WIDTH; + result.left.h = result.left.w = BALL_HEIGHT; + return result; +} + +void render_eyes(SDL_Window *window,SDL_Texture *eyes_texture,Pupil_Pair pupils) { + SDL_SelectRenderer(window); + + //Clear render-target to blue. + SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff); + SDL_RenderClear(); + + //Render the whites of the eyes. + SDL_Rect srcdestrect = {0,0,eyes_width,eyes_height}; + SDL_RenderCopy(eyes_texture,&srcdestrect,&srcdestrect); + + //Render the pupils. + SDL_SetRenderDrawColor(0x00,0x00,0x00,0xff); + const SDL_Rect eyes[2] = {pupils.left,pupils.right}; + SDL_RenderFillRects((const SDL_Rect**)&eyes,2); + + SDL_RenderPresent(); +} + +int main(int argc,char** argv) { + if(SDL_VideoInit(NULL,0) == -1) { + printf("Could not initialize SDL video.\n"); + exit(-1); + } + + SDL_Window *window = SDL_CreateShapedWindow("Big Brother is watching you.",eyes_x_hot,eyes_y_hot,eyes_width,eyes_height,SDL_WINDOW_RESIZABLE); + if(window == NULL) { + SDL_VideoQuit(); + printf("Could not create shaped window for eyes.\n"); + exit(-2); + } + if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) { + SDL_DestroyWindow(window); + SDL_VideoQuit(); + printf("Could not create rendering context for SDL_Eyes window.\n"); + exit(-3); + } + + SDL_Color bnw_palette[2] = {{0,0,0,0},{255,255,255,255}}; + SDL_Texture *eyes_texture = SDL_CreateTexture(SDL_PIXELFORMAT_INDEX1LSB,SDL_TEXTUREACCESS_STREAMING,eyes_width,eyes_height); + if(eyes_texture == NULL) { + SDL_DestroyRenderer(window); + SDL_DestroyWindow(window); + SDL_VideoQuit(); + printf("Could not create eyes texture.\n"); + exit(-4); + } + SDL_SetTexturePalette(eyes_texture,bnw_palette,0,2); + + void *pixels = NULL; + int pitch = 0; + SDL_Rect rect = {0,0,eyes_width,eyes_height}; + SDL_LockTexture(eyes_texture,&rect,1,&pixels,&pitch); + for(int row = 0;row