AnimTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Windows (GDI) Test Source
  4. // Copyright (C) 2000 by Denton Woods
  5. // Last modified: 08/26/2001 <--Y2K Compliant! =]
  6. //
  7. // Filename: testil/animtest/animtest.c
  8. //
  9. // Description: Animation test application for DevIL.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include <windows.h>
  13. #ifdef _DEBUG
  14. #define IL_DEBUG
  15. #endif
  16. #include <il/il.h>
  17. #include <il/ilu.h>
  18. #include <il/ilut.h>
  19. #include "resource.h"
  20. // Evil globals!
  21. HINSTANCE hInstance;
  22. HDC hDC, hMemDC = NULL;
  23. HWND HWnd;
  24. #define BORDER_W 8
  25. #define MENU_H 46
  26. #define MIN_W 205 // Accomodate the menu bar.
  27. #define MAX_W 400
  28. #define MAX_H 400
  29. #define TITLE "DevIL Animation Test"
  30. ILuint FilterType;
  31. ILuint FilterParamInt;
  32. ILfloat FilterParamFloat;
  33. char FilterEditString[255];
  34. char NewTitle[512];
  35. BITMAPINFOHEADER *BmpInfo = NULL;
  36. HBITMAP *Bitmaps = NULL;
  37. ILuint *Durations = NULL;
  38. ILuint NumImages = 0, CurImage = 0;
  39. __int64 StartTime, TimerFreq;
  40. double TimerRes;
  41. bool IsPaused = false;
  42. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  43. void GenFilterString(char *Out, char **Strings);
  44. void DisplayImage(void);
  45. void LoadImages(char *FileName);
  46. int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  47. {
  48. MSG msg;
  49. WNDCLASSEX wcex;
  50. HACCEL hAccelTable;
  51. hInstance = hInst;
  52. wcex.cbSize = sizeof(WNDCLASSEX);
  53. wcex.style = CS_HREDRAW | CS_VREDRAW;
  54. wcex.lpfnWndProc = (WNDPROC)WndProc;
  55. wcex.cbClsExtra = 0;
  56. wcex.cbWndExtra = 0;
  57. wcex.hInstance = hInstance;
  58. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
  59. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  60. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  61. wcex.lpszMenuName = (LPCSTR)IDR_MENU1;
  62. wcex.lpszClassName = TITLE;
  63. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON1);
  64. RegisterClassEx(&wcex);
  65. HWnd = CreateWindow(TITLE, TITLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  66. 50, 50, 400, 300, NULL, NULL, hInstance, NULL);
  67. if (HWnd == NULL)
  68. return FALSE;
  69. // Display the window
  70. ShowWindow(HWnd, nCmdShow);
  71. UpdateWindow(HWnd);
  72. ilInit();
  73. ilEnable(IL_ORIGIN_SET);
  74. ilEnable(IL_TYPE_SET);
  75. ilEnable(IL_FORMAT_SET);
  76. ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
  77. ilTypeFunc(IL_UNSIGNED_BYTE);
  78. ilFormatFunc(IL_BGR);
  79. // Is there a file to load from the command-line?
  80. if (__argc > 1) {
  81. LoadImages(__argv[1]);
  82. }
  83. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDR_MENU1);
  84. while (GetMessage(&msg, NULL, 0, 0)) {
  85. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
  86. TranslateMessage(&msg);
  87. DispatchMessage(&msg);
  88. }
  89. }
  90. return msg.wParam;
  91. }
  92. void LoadImages(char *FileName)
  93. {
  94. ILuint Image, i;
  95. hDC = GetDC(HWnd);
  96. hMemDC = CreateCompatibleDC(hDC);
  97. ilGenImages(1, &Image);
  98. ilBindImage(Image);
  99. if (!ilLoadImage(FileName)) {
  100. ilDeleteImages(1, &Image);
  101. return;
  102. }
  103. ilEnable(IL_ORIGIN_SET);
  104. ilEnable(IL_FORMAT_SET);
  105. ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
  106. //ilFormatFunc(IL_BGRA);
  107. ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
  108. ilutRenderer(ILUT_WIN32);
  109. CurImage = 0;
  110. NumImages = ilGetInteger(IL_NUM_IMAGES) + 1;
  111. Bitmaps = new HBITMAP[NumImages];
  112. BmpInfo = new BITMAPINFOHEADER[NumImages];
  113. Durations = new ILuint[NumImages];
  114. if (Bitmaps == NULL || BmpInfo == NULL || Durations == NULL) {
  115. ilDeleteImages(1, &Image);
  116. return;
  117. }
  118. for (i = 0; i < NumImages; i++) {
  119. ilActiveImage(0);
  120. ilActiveImage(i);
  121. Durations[i] = ilGetInteger(IL_IMAGE_DURATION);
  122. *(Bitmaps + i) = ilutConvertToHBitmap(hDC);
  123. ilutGetBmpInfo((BITMAPINFO*)(BmpInfo + i));
  124. }
  125. SelectObject(hMemDC, Bitmaps[0]);
  126. ilDeleteImages(1, &Image);
  127. sprintf(NewTitle, "%s - %s", TITLE, FileName);
  128. SetWindowText(HWnd, NewTitle);
  129. QueryPerformanceFrequency((LARGE_INTEGER*)&TimerFreq);
  130. TimerRes = 1.0 / TimerFreq;
  131. QueryPerformanceCounter((LARGE_INTEGER*)&StartTime);
  132. return;
  133. }
  134. void DestroyGDI()
  135. {
  136. ILuint i;
  137. if (Bitmaps) {
  138. for (i = 0; i < NumImages; i++) {
  139. DeleteObject(*(Bitmaps + i));
  140. }
  141. }
  142. if (hMemDC)
  143. DeleteDC(hMemDC);
  144. if (Bitmaps)
  145. delete []Bitmaps;
  146. if (BmpInfo)
  147. delete []BmpInfo;
  148. Bitmaps = NULL;
  149. BmpInfo = NULL;
  150. hMemDC = NULL;
  151. return;
  152. }
  153. void DisplayImage()
  154. {
  155. static PAINTSTRUCT ps;
  156. static __int64 CurTime;
  157. static double TimeElapsed;
  158. // Not created yet.
  159. if (Durations == NULL || BmpInfo == NULL || Bitmaps == NULL)
  160. return;
  161. if (!IsPaused) {
  162. QueryPerformanceCounter((LARGE_INTEGER*)&CurTime);
  163. TimeElapsed = (CurTime - StartTime) * TimerRes;
  164. if (TimeElapsed * 1000 > Durations[CurImage]) {
  165. StartTime = CurTime;
  166. CurImage++;
  167. if (CurImage >= NumImages) {
  168. CurImage = 0;
  169. }
  170. SelectObject(hMemDC, Bitmaps[CurImage]);
  171. }
  172. }
  173. hDC = BeginPaint(HWnd, &ps);
  174. BitBlt(hDC, 0, 0, (WORD)BmpInfo[CurImage].biWidth, (WORD)BmpInfo[CurImage].biHeight,
  175. hMemDC, 0, 0, SRCCOPY);
  176. EndPaint(HWnd, &ps);
  177. return;
  178. }
  179. // Window procedure, handles all messages for this program
  180. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  181. {
  182. static HMENU hMenu;
  183. static ILuint Colours;
  184. static RECT Rect;
  185. static HDROP hDrop;
  186. static char OpenFileName[2048];
  187. static char OpenFilter[2048];
  188. static char *OFilter[] = {
  189. "All Files (*.*)", "*.*",
  190. "Half-Life Model Files (*.mdl)", "*.mdl",
  191. "Homeworld Image Files (*.lif)", "*.lif",
  192. "Image Files (All Supported Types)", "*.jpe;*.jpg;*.jpeg;*.lif;*.bmp;*.ico;*.pbm;*.pgm;*.pnm;*.ppm;*.png;*.bw;*.rgb;*.rgba;*.sgi;*.tga;*.tif;*.tiff;*.pcx",
  193. "Jpeg Files (*.jpe, *.jpg, *.jpeg)", "*.jpe;*.jpg;*.jpeg",
  194. "Microsoft Bitmap Files (*.bmp)", "*.bmp",
  195. "Microsoft Icon Files (*.ico)", "*.ico",
  196. "OpenIL Files (*.oil)", "*.oil",
  197. "Portable AnyMap Files (*.pbm, *.pgm, *.pnm, *.ppm)", "*.pbm;*.pgm;*.pnm;*.ppm",
  198. "Portable Network Graphics Files (*.png)", "*.png",
  199. "Sgi Files (*.sgi)", "*.bw;*.rgb;*.rgba;*.sgi",
  200. "Targa Files (*.tga)", "*.tga",
  201. "Tiff Files (*.tif)", "*.tif;*.tiff",
  202. "Quake Wal Files (*.wal)", "*.wal",
  203. "ZSoft Pcx Files (*.pcx)", "*.pcx",
  204. "\0\0"
  205. };
  206. static OPENFILENAME Ofn = {
  207. sizeof(OPENFILENAME),
  208. hWnd,
  209. NULL,
  210. OpenFilter,
  211. NULL,
  212. 0,
  213. 0,
  214. OpenFileName,
  215. 512,
  216. NULL,
  217. 0,
  218. NULL,
  219. NULL,
  220. OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST,
  221. 0,
  222. 0,
  223. NULL,
  224. NULL,
  225. NULL,
  226. NULL
  227. };
  228. switch (message)
  229. {
  230. case WM_CREATE:
  231. GenFilterString(OpenFilter, OFilter);
  232. hDC = GetDC(hWnd);
  233. DragAcceptFiles(hWnd, TRUE);
  234. break;
  235. case WM_CLOSE:
  236. DestroyGDI();
  237. ReleaseDC(hWnd, hDC);
  238. DestroyWindow(hWnd);
  239. UnregisterClass(TITLE, hInstance);
  240. break;
  241. case WM_DESTROY:
  242. PostQuitMessage(0);
  243. break;
  244. case WM_PAINT:
  245. DisplayImage();
  246. InvalidateRect(hWnd, NULL, FALSE);
  247. break;
  248. case WM_KEYDOWN:
  249. if (wParam == VK_ESCAPE)
  250. PostQuitMessage(0);
  251. InvalidateRect(hWnd, NULL, FALSE);
  252. break;
  253. case WM_DROPFILES:
  254. hDrop = (HDROP)wParam;
  255. DragQueryFile(hDrop, 0, OpenFileName, 512);
  256. DestroyGDI();
  257. LoadImages(OpenFileName);
  258. DragFinish (hDrop);
  259. return 0;
  260. case WM_COMMAND:
  261. FilterType = LOWORD(wParam);
  262. switch (LOWORD(wParam))
  263. {
  264. case ID_FILE_EXIT:
  265. PostMessage(hWnd, WM_CLOSE, 0, 0);
  266. return (0L);
  267. case ID_FILE_LOAD:
  268. sprintf(OpenFileName, "*.*");
  269. Ofn.lpstrFilter = OpenFilter;
  270. Ofn.lpstrFile = OpenFileName;
  271. Ofn.lpstrTitle = "Open File";
  272. Ofn.nFilterIndex = 1;
  273. Ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
  274. if (!GetOpenFileName(&Ofn))
  275. return (0L);
  276. DestroyGDI();
  277. LoadImages(OpenFileName);
  278. return (0L);
  279. case ID_EDIT_PAUSE:
  280. IsPaused = true;
  281. return 0;
  282. case ID_EDIT_RESUME:
  283. IsPaused = false;
  284. QueryPerformanceCounter((LARGE_INTEGER*)&StartTime);
  285. return 0;
  286. }
  287. default:
  288. return (DefWindowProc(hWnd, message, wParam, lParam));
  289. }
  290. return (0L);
  291. }
  292. void GenFilterString(char *Out, char **Strings)
  293. {
  294. int OutPos = 0, StringPos = 0;
  295. while (Strings[StringPos][0] != 0) {
  296. sprintf(Out + OutPos, Strings[StringPos]);
  297. OutPos += strlen(Strings[StringPos++]) + 1;
  298. }
  299. Out[OutPos++] = 0;
  300. Out[OutPos] = 0;
  301. return;
  302. }