comparison 3x666.c @ 13:3a358d053ffc

Runs now, audio does not work yet and the pace seems slightly too fast (not sure if that is the case however.)
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Mar 2013 17:27:26 +0200
parents fcae85b39931
children 2663b7bb23b9
comparison
equal deleted inserted replaced
12:df40b5e4ca71 13:3a358d053ffc
1 #include <SDL.h> 1 #include <SDL.h>
2 #include <math.h> 2 #include <math.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <unistd.h> 4 #include <unistd.h>
5 #include <string.h> 5 #include <string.h>
6 #include <stdarg.h>
6 #include "config.h" 7 #include "config.h"
7 #include "3xfont.h" 8 #include "3xfont.h"
8 9
9 10
10 /** typedefs **/ 11 /** typedefs **/
18 # else 19 # else
19 # define BOOL int 20 # define BOOL int
20 # endif 21 # endif
21 #endif 22 #endif
22 23
24
23 struct 25 struct
24 { 26 {
25 int tickLen; 27 int tickLen;
26 int frameTime, frameCount, 28 int frameTime, frameCount,
27 startTime, endTime; 29 startTime, endTime;
47 49
48 static inline int dmClamp(const int v, const int min, const int max) 50 static inline int dmClamp(const int v, const int min, const int max)
49 { 51 {
50 return (v < min ? min : (v > max ? max : v)); 52 return (v < min ? min : (v > max ? max : v));
51 } 53 }
54
55
56 static int engineGetTick()
57 {
58 return ((engine.frameTime - engine.startTime) * SET_DEMOHZ) / 1000;
59 }
60
61
62 void dmErrorVA(const char *fmt, va_list ap)
63 {
64 fprintf(stderr, "3x666: ");
65 vfprintf(stderr, fmt, ap);
66 }
67
68
69 void dmError(const char *fmt, ...)
70 {
71 va_list ap;
72
73 va_start(ap, fmt);
74 dmErrorVA(fmt, ap);
75 va_end(ap);
76 }
77
52 78
79 static BOOL engineInitializeVideo()
80 {
81 SDL_FreeSurface(engine.screen);
82
83 engine.screen = SDL_SetVideoMode(SET_VID_BUFW, SET_VID_BUFH, 8, engine.optVFlags);
84 if (engine.screen == NULL)
85 {
86 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
87 return FALSE;
88 }
89 return TRUE;
90 }
91
92
53 /**************** tEXT dRAWiNG rOUTiNES ********** 93 /**************** tEXT dRAWiNG rOUTiNES **********
54 94
55 bitmaps are for lamers :) let's use a little 12-segment calculator font... 95 bitmaps are for lamers :) let's use a little 12-segment calculator font...
56 ascii chars 32..90, 16 bits per char unpacked -> 114 bytes for the whole 96 ascii chars 32..90, 16 bits per char unpacked -> 114 bytes for the whole
57 font ;) let's credit karl/nooon for the original idea. */ 97 font ;) let's credit karl/nooon for the original idea. */
217 ddot += dddot; 257 ddot += dddot;
218 *d++ = dotxyz >> 16; 258 *d++ = dotxyz >> 16;
219 } 259 }
220 } 260 }
221 261
222 void drawball(Uint8 *d, vec3d * l, int relR) 262 void drawball(SDL_Surface *screen, vec3d * l, int relR)
223 { 263 {
224 int R = balltab[relR].R, *s = balltab[relR].tab; 264 int R = balltab[relR].R, *s = balltab[relR].tab;
225 265
226 signed int doty = (-(R - 1) * l->y); 266 signed int doty = (-(R - 1) * l->y);
227 signed int y = R * 2 - 2; 267 signed int y = R * 2 - 2;
228 268
229 d += (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * SET_VID_BUFW; 269 Uint8 *dp = screen->pixels + (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * screen->pitch;
230 270
231 if (y) 271 for (; y; y--)
232 { 272 {
233 for (; y; y--) 273 int halfw = *s++;
234 { 274 if (halfw)
235 int halfw = *s++; 275 {
236 if (halfw) 276 drawball_inloop(dp + R - halfw,
237 drawball_inloop(d + R - halfw, 277 (doty - (l->x * halfw)) << 8,
238 (doty - (l->x * halfw)) << 8, 278 (l->x + l->z) << 8,
239 (l->x + l->z) << 8, 279 0 - ((l->z << 8) / halfw), halfw << 1);
240 0 - ((l->z << 8) / halfw), halfw << 1); 280 }
241 d += SET_VID_BUFW; 281 dp += screen->pitch;
242 doty += l->y; 282 doty += l->y;
243 }
244 } 283 }
245 } 284 }
246 285
247 /* some extra for freaks: a plasma made with the phongball innerloop :) 286 /* some extra for freaks: a plasma made with the phongball innerloop :)
248 looks ugly. 287 looks ugly.
834 int flagz = 0; 873 int flagz = 0;
835 const short *dez = dezign; 874 const short *dez = dezign;
836 char *phiword = NULL, *dizainword = NULL; 875 char *phiword = NULL, *dizainword = NULL;
837 int flixtim = 0; 876 int flixtim = 0;
838 877
878 // engine.optVFlags |= SDL_FULLSCREEN;
879
880
881 // Initialize SDL components
839 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) 882 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0)
840 { 883 {
841 dmError("Could not initialize SDL: %s\n", SDL_GetError()); 884 dmError("Could not initialize SDL: %s\n", SDL_GetError());
842 goto error_exit; 885 goto error_exit;
843 } 886 }
844 initSDL = TRUE; 887 initSDL = TRUE;
845 888
889 // Initialize audio parts
846 engine.optAfmt.freq = SET_AUDIO_FREQ; 890 engine.optAfmt.freq = SET_AUDIO_FREQ;
847 engine.optAfmt.format = AUDIO_S16SYS; 891 engine.optAfmt.format = AUDIO_S16SYS;
848 engine.optAfmt.channels = SET_AUDIO_CHN; 892 engine.optAfmt.channels = SET_AUDIO_CHN;
849 engine.optAfmt.samples = engine.optAfmt.freq / 16; 893 engine.optAfmt.samples = engine.optAfmt.freq / 16;
850 engine.optAfmt.callback = engineAudioCallback; 894 engine.optAfmt.callback = engineAudioCallback;
851 895
896 // Initialize SDL audio
852 if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0) 897 if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0)
853 { 898 {
854 dmError("Couldn't open SDL audio: %s\n", SDL_GetError()); 899 dmError("Couldn't open SDL audio: %s\n", SDL_GetError());
855 } 900 }
856 901
857 // foo 902 // Initialize SDL video
858 engine.screen = SDL_SetVideoMode(SET_VID_BUFW, SET_VID_BUFH, 8, engine.optVFlags); 903 if (!engineInitializeVideo())
859 if (engine.screen == NULL) 904 goto error_exit;
860 {
861 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
862 return FALSE;
863 }
864 905
865 SDL_ShowCursor(SDL_DISABLE); 906 SDL_ShowCursor(SDL_DISABLE);
866 SDL_WM_SetCaption(SET_WIN_NAME, SET_WIN_NAME); 907 SDL_WM_SetCaption(SET_WIN_NAME, SET_WIN_NAME);
867 908
909 // Initialize effects
868 preball(); 910 preball();
869 srand(0); 911 srand(0);
870 912
871 engine.tickLen = engine.optAfmt.freq / SET_DEMOHZ; 913 engine.tickLen = engine.optAfmt.freq / SET_DEMOHZ;
872 audio_precalcs(); 914 audio_precalcs();
873 setpal(); 915 setpal();
874 916
917 // Start audio, enter main loop
875 SDL_LockAudio(); 918 SDL_LockAudio();
876 SDL_PauseAudio(0); 919 SDL_PauseAudio(0);
877 SDL_UnlockAudio(); 920 SDL_UnlockAudio();
878 engine.startTime = SDL_GetTicks(); 921 engine.startTime = SDL_GetTicks();
879 922
880 while (!engine.exitFlag) 923 while (!engine.exitFlag)
881 { 924 {
925 // Handle SDL events
882 while (SDL_PollEvent(&engine.event)) 926 while (SDL_PollEvent(&engine.event))
883 switch (engine.event.type) 927 switch (engine.event.type)
884 { 928 {
885 case SDL_KEYDOWN: 929 case SDL_KEYDOWN:
886 switch (engine.event.key.keysym.sym) 930 switch (engine.event.key.keysym.sym)
918 break; 962 break;
919 } 963 }
920 964
921 // Draw frame 965 // Draw frame
922 engine.frameTime = SDL_GetTicks(); 966 engine.frameTime = SDL_GetTicks();
967 int qt = engineGetTick(&engine);
923 968
924 if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0) 969 if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0)
925 { 970 {
926 dmError("Can't lock surface.\n"); 971 dmError("Can't lock surface.\n");
927 goto error_exit; 972 goto error_exit;
928 } 973 }
929 974
930 // Main rendering code 975 // Main rendering code
931 976 while ((qt / SET_ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
932 while ((engine.frameTime / SET_ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
933 { 977 {
934 dez++; 978 dez++;
935 flagz = *dez++; 979 flagz = *dez++;
936 if (flagz & FLASHTXT) 980 if (flagz & FLASHTXT)
937 flixtim = *(dez - 2); 981 flixtim = *(dez - 2);
939 dizainword = dotxtscr(); 983 dizainword = dotxtscr();
940 } 984 }
941 985
942 if (flagz & FLASHTXT) 986 if (flagz & FLASHTXT)
943 { 987 {
944 while ((engine.frameTime / SET_ROWTIX) >= flixtim) 988 while ((qt / SET_ROWTIX) >= flixtim)
945 { 989 {
946 phiword = lyrix(); 990 phiword = lyrix();
947 flixtim += 4; 991 flixtim += 4;
948 } 992 }
949 } 993 }
956 SDL_FillRect(engine.screen, NULL, 0); 1000 SDL_FillRect(engine.screen, NULL, 0);
957 } 1001 }
958 else 1002 else
959 if (flagz & FLASHBG) 1003 if (flagz & FLASHBG)
960 { 1004 {
961 unsigned char col = 130 + (t % 48) * 2; 1005 unsigned char col = 130 + (qt % 48) * 2;
962 SDL_FillRect(engine.screen, NULL, col); 1006 SDL_FillRect(engine.screen, NULL, col);
963 } 1007 }
964 1008
965 if (flagz & CHESSBG) 1009 if (flagz & CHESSBG)
966 { 1010 {
967 int zoom = (10 + abs(((t >> 1) % 96) - 48)) * 4096 / SET_VID_BUFW; 1011 int zoom = (10 + abs(((qt >> 1) % 96) - 48)) * 4096 / SET_VID_BUFW;
968 rotochess(engine.screen, sin(t * 0.03) * zoom, cos(t * 0.03) * zoom, 0, 0); 1012 rotochess(engine.screen, sin(qt * 0.03) * zoom, cos(qt * 0.03) * zoom, 0, 0);
969 } 1013 }
970 1014
971 /* if(flagz&PLASMABG) drawplasma(ruutu,t); */ 1015 /* if(flagz&PLASMABG) drawplasma(ruutu,t); */
972 1016
973 if (flagz & OCSALOGO) 1017 if (flagz & OCSALOGO)
974 { 1018 {
975 drawchar(U * 6, U * 4, phont['O' - 32], U + U * sin(t * 0.10 + 3), 1019 drawchar(U * 6, U * 4, phont['O' - 32], U + U * sin(qt * 0.10 + 3),
976 U); 1020 U);
977 drawchar(U * 14, U * 4, phont['C' - 32], U, 1021 drawchar(U * 14, U * 4, phont['C' - 32], U,
978 U + U * sin(t * 0.11 + 3)); 1022 U + U * sin(qt * 0.11 + 3));
979 drawchar(U * 22, U * 4, phont['S' - 32], U, 1023 drawchar(U * 22, U * 4, phont['S' - 32], U,
980 U + U * sin(t * 0.12 + 3)); 1024 U + U * sin(qt * 0.12 + 3));
981 drawchar(U * 30, U * 4, phont['A' - 32], 1025 drawchar(U * 30, U * 4, phont['A' - 32],
982 U + U * sin(t * 0.13 + 3), U); 1026 U + U * sin(qt * 0.13 + 3), U);
983 } 1027 }
984 1028
985 if (flagz & SCROLL0) 1029 if (flagz & SCROLL0)
986 plainscroll(t); 1030 plainscroll(qt);
987 1031
988 if (flagz & BALLIE) 1032 if (flagz & BALLIE)
989 { 1033 {
990 int zoom; 1034 int zoom;
991 if (flagz & BALLJUMPS) 1035 if (flagz & BALLJUMPS)
992 zoom = abs((t % 96) - 48); 1036 zoom = abs((qt % 96) - 48);
993 else 1037 else
994 zoom = 47; 1038 zoom = 47;
995 1039
996 zoom = dmClamp(zoom, 0, 47); 1040 zoom = dmClamp(zoom, 0, 47);
997 1041
998 unitvec(&joo, 0.038 * t, 0.023 * t, 0.011 * t, 1042 unitvec(&joo, 0.038 * qt, 0.023 * qt, 0.011 * qt,
999 32000 / balltab[zoom].R); 1043 32000 / balltab[zoom].R);
1000 joo.z <<= 1; 1044 joo.z <<= 1;
1001 1045
1002 drawball(engine.screen, &joo, zoom); 1046 drawball(engine.screen, &joo, zoom);
1003 } 1047 }
1004 1048
1005 if (flagz & FLASHTXT) 1049 if (flagz & FLASHTXT)
1006 flashtxt(phiword); 1050 flashtxt(phiword);
1007 1051
1008 if ((flagz & TXTSCR) && ((t / SET_ROWTIX) & 2)) 1052 if ((flagz & TXTSCR) && ((qt / SET_ROWTIX) & 2))
1009 drawtxtscr(dizainword); 1053 drawtxtscr(dizainword);
1010 1054
1011 if (flagz & ENDSCR) 1055 if (flagz & ENDSCR)
1012 doendscroll(t - 1024 * SET_ROWTIX); 1056 doendscroll(qt - 1024 * SET_ROWTIX);
1013 1057
1014 if (flagz & COUNTAH) 1058 if (flagz & COUNTAH)
1015 { 1059 {
1016 int n = ((t * 50 / 48) - 256 * 6); 1060 int n = ((qt * 50 / 48) - 256 * 6);
1017 int dis = (rand() % U) >> 1; 1061 int dis = (rand() % U) >> 1;
1018 if (n > 666) 1062 if (n > 666)
1019 n = 666; 1063 n = 666;
1020 1064
1021 if (n > 600) 1065 if (n > 600)