diff --git a/sfxr/Makefile.am b/sfxr/Makefile.am index 3b61eb3..d861438 100644 --- a/sfxr/Makefile.am +++ b/sfxr/Makefile.am @@ -1,10 +1,10 @@ AUTOMAKE_OPTIONS = foreign CFLAGS = --pedantic -Wall -O2 -CXXFLAGS=$(CFLAGS) `sdl-config --cflags` `pkg-config gtk+-2.0 --cflags` +CXXFLAGS=$(CFLAGS) `sdl-config --cflags` CPPFLAGS=-DDATADIR='$(pkgdatadir)' -LDFLAGS=`sdl-config --libs` `pkg-config gtk+-2.0 --libs` +LDFLAGS=`sdl-config --libs` bin_PROGRAMS = sfxr -sfxr_SOURCES := main.cpp ddrawkit.h sdlkit.h tools.h +sfxr_SOURCES := main.cpp sdlkit.cpp tools.cpp sdlkit.h tools.h diff --git a/sfxr/ddrawkit.h b/sfxr/ddrawkit.h deleted file mode 100644 index ad3aa54..0000000 --- a/sfxr/ddrawkit.h +++ /dev/null @@ -1,390 +0,0 @@ -/* - Copyright (c) 2007 Tomas Pettersson - - 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 - AUTHORS OR COPYRIGHT HOLDERS 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. - - - Porting notes - ------------- - - Need to provide window/framebuffer setup at program start - - ddkUnlock() is called when the window should be redrawn - - WinMain() at the bottom of this file is the entry point and main loop - which handles messages and calling the app update function ddkCalcFrame() - - ddkscreen32 = pointer to 640x480 DWORD pixel buffer - mouse_* = mouse info, only need x/y/px/py/left/right/leftclick/rightclick - -*/ - - -#define _WIN32_WINDOWS 0xBAD -#include - -extern "C" long _ftol( double ); //defined by VC6 C libs -extern "C" long _ftol2( double dblSource ) { return _ftol( dblSource ); } - -void ddkInit(); // Will be called on startup -bool ddkCalcFrame(); // Will be called every frame, return true to continue running or false to quit -void ddkFree(); // Will be called on shutdown -bool ddkLock(); // Call immediately before drawing (once per frame) -void ddkUnlock(); // Call immediately after drawing (once per frame) -void ddkDrawPixel(int x, int y, int red, int green, int blue); // Draw a pixel -void ddkSetMode(int width, int height, int bpp, int refreshrate, int fullscreen, char *title); -#define DDK_WINDOW 0 // Run in a normal resizable window (stretch to fit) -#define DDK_FULLSCREEN 1 // Change display mode to the one specified, use vsync -#define DDK_STRETCH 2 // Run in a maximized window, looks like fullscreen -int ddkGetBpp(); // Use this to find out the current color depth -DWORD *ddkscreen32; // Use this for 32 bit modes -WORD *ddkscreen16; // Use this for 16 bit modes -int ddkpitch; // Offset in pixels from the start of one horizontal line to the start of the next - -bool ddrawkit_initialised; -bool ddrawkit_released; -bool ddrawkit_fullscreen; -bool ddrawkit_fullwindow; -int ddrawkit_width; -int ddrawkit_height; -int ddrawkit_bpp; -int ddrawkit_refresh; -bool ddrawkit_timeravailable; - -HWND hWndMain; -HINSTANCE hInstanceMain; - -HCURSOR cursor_arrow; -int mouse_x, mouse_y, mouse_px, mouse_py; -bool mouse_left=false, mouse_right=false, mouse_middle=false; -bool mouse_leftclick=false, mouse_rightclick=false, mouse_middleclick=false; -bool mouse_doubleclick=false; -int mouse_wheel=0; - - -// --- DDB implementation - -struct pBITMAPINFO -{ - BITMAPINFOHEADER bmiHeader; - RGBQUAD bmiColors[256]; -} BMInfo; - -HBITMAP hBM; - -HDC hDC_comp; -DWORD* image_bitmap; - -void InitImageBuffer() -{ - HPALETTE PalHan; - HWND ActiveWindow; - HDC hDC; - - image_bitmap=(DWORD*)malloc(ddrawkit_width*ddrawkit_height*sizeof(DWORD)); - - ActiveWindow=hWndMain; - hDC=GetDC(ActiveWindow); - - BMInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); - BMInfo.bmiHeader.biWidth=ddrawkit_width; - BMInfo.bmiHeader.biHeight=-abs(ddrawkit_height); - BMInfo.bmiHeader.biPlanes=1; - BMInfo.bmiHeader.biBitCount=32; - BMInfo.bmiHeader.biCompression=BI_RGB; - BMInfo.bmiHeader.biSizeImage=ddrawkit_width*ddrawkit_height*4; - BMInfo.bmiHeader.biXPelsPerMeter=0; - BMInfo.bmiHeader.biYPelsPerMeter=0; - - hBM=CreateDIBSection(hDC, (BITMAPINFO*)&BMInfo, DIB_RGB_COLORS, (void**)&image_bitmap, 0, 0); - ReleaseDC(ActiveWindow, hDC); - - ddkscreen32=image_bitmap; - ddkpitch=ddrawkit_width; -} - -void DestroyImageBuffer() -{ - free(image_bitmap); -} - -void ddrawkit_BlitWindowDDB() -{ - HDC hDC; - HBITMAP DefaultBitmap; - - hDC=GetDC(hWndMain); - DefaultBitmap=(HBITMAP)SelectObject(hDC_comp, hBM); -// BitBlt(hDC, x1, y1, x2-x1, y2-y1, hDC_comp, x1, y1, SRCCOPY); - BitBlt(hDC, 0, 0, ddrawkit_width, ddrawkit_height, hDC_comp, 0, 0, SRCCOPY); - SelectObject(hDC_comp, DefaultBitmap); - DeleteDC(hDC); -} - -// --- DDB implementation - - -void ddrawkit_SafeDestroy() -{ - if(!ddrawkit_released) - { - ddrawkit_released=true; - ddkFree(); - DestroyWindow(hWndMain); - DestroyImageBuffer(); - } -} - -LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - switch (uMsg) - { - case WM_CLOSE: - ddrawkit_SafeDestroy(); - return 0; - - case WM_SETCURSOR: - if(ddrawkit_fullscreen || ddrawkit_fullwindow) - { - SetCursor(NULL); - return 0; - } - else - SetCursor(cursor_arrow); - break; - - case WM_MOUSEMOVE: - int curwidth, curheight; - RECT rect; - GetClientRect(hWndMain, &rect); - curwidth=rect.right-rect.left; - curheight=rect.bottom-rect.top; - mouse_x=(int)((float)LOWORD(lParam)/curwidth*ddrawkit_width); - mouse_y=(int)((float)HIWORD(lParam)/curheight*ddrawkit_height); - return 0; - case WM_MOUSEWHEEL: - if((wParam>>16)&0x7FFF) - { - if((wParam>>16)&0x8000) - mouse_wheel=-1; - else - mouse_wheel=1; - } - return 0; - case WM_LBUTTONDBLCLK: - mouse_doubleclick=true; - return 0; - case WM_LBUTTONDOWN: - mouse_left=true; - mouse_leftclick=true; - return 0; - case WM_LBUTTONUP: - mouse_left=false; - return 0; - case WM_RBUTTONDOWN: - mouse_right=true; - mouse_rightclick=true; - return 0; - case WM_RBUTTONUP: - mouse_right=false; - return 0; - case WM_MBUTTONDOWN: - mouse_middle=true; - mouse_middleclick=true; - return 0; - case WM_MBUTTONUP: - mouse_middle=false; - return 0; - - case WM_DESTROY: - PostQuitMessage(0); - return 0; - } - return DefWindowProc(hWnd, uMsg, wParam, lParam); -} - -void ddrawkit_RegisterWindowClass() -{ - WNDCLASSEX wcx; - - ZeroMemory(&wcx, sizeof(WNDCLASSEX)); - wcx.cbSize=sizeof(WNDCLASSEX); - wcx.lpfnWndProc=MainWindowProc; - wcx.hInstance=GetModuleHandle(NULL); - wcx.hIcon=LoadIcon(NULL, IDI_APPLICATION); - wcx.hCursor=NULL; -// wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); - wcx.hbrBackground=NULL; - wcx.lpszMenuName=NULL; - wcx.lpszClassName="DDrawKitClass"; - RegisterClassEx(&wcx); -} - -bool ddkLock() -{ - return true; -} - -void ddkUnlock() -{ - ddrawkit_BlitWindowDDB(); -} - -void ddkDrawPixel(int x, int y, int red, int green, int blue) -{ - if(x<0 || y<0 || x>=ddrawkit_width || y>=ddrawkit_height) - return; - - if(ddrawkit_bpp==32) - { - DWORD color=(red<<16)|(green<<8)|blue; - ddkscreen32[y*ddkpitch+x]=color; - } - if(ddrawkit_bpp==16) - { - WORD color=((red>>3)<<11)|((green>>2)<<5)|(blue>>3); - ddkscreen16[y*ddkpitch+x]=color; - } -} - -void ddkSetMode(int width, int height, int bpp, int refreshrate, int fullscreen, char *title) -{ - if(!ddrawkit_initialised) - { - ddrawkit_width=width; - ddrawkit_height=height; - ddrawkit_bpp=bpp; - ddrawkit_refresh=refreshrate; - switch(fullscreen) - { - case 0: - ddrawkit_fullscreen=false; - ddrawkit_fullwindow=false; - break; - case 1: - ddrawkit_fullscreen=true; - ddrawkit_fullwindow=false; - break; - case 2: - ddrawkit_fullscreen=false; - ddrawkit_fullwindow=true; - break; - } - - ddrawkit_RegisterWindowClass(); - - if(ddrawkit_fullscreen || ddrawkit_fullwindow) - { - // Screen sized window (possibly stretched) - hWndMain=CreateWindowEx(WS_EX_APPWINDOW, - "DDrawKitClass", title, WS_POPUP, - 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), - NULL, NULL, hInstanceMain, NULL); - } - else - { - // Normal window -// hWndMain=CreateWindow("DDrawKitClass", title, WS_OVERLAPPEDWINDOW, - hWndMain=CreateWindow("DDrawKitClass", title, WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX, -// 0, 0, ddrawkit_width+GetSystemMetrics(SM_CXSIZEFRAME)*2, -// ddrawkit_height+GetSystemMetrics(SM_CYSIZEFRAME)*2+GetSystemMetrics(SM_CYCAPTION), - GetSystemMetrics(SM_CXSCREEN)/2-320, GetSystemMetrics(SM_CYSCREEN)/2-300, - ddrawkit_width+GetSystemMetrics(SM_CXEDGE)*2, - ddrawkit_height+GetSystemMetrics(SM_CYEDGE)*2+GetSystemMetrics(SM_CYCAPTION), - NULL, NULL, hInstanceMain, NULL); - } - - // init DDB implementation - hDC_comp=CreateCompatibleDC(NULL); - InitImageBuffer(); - - ShowWindow(hWndMain, SW_SHOW); - - ddrawkit_initialised=true; - ddrawkit_released=false; - - // Clear screen - ddkLock(); - for(int y=0;y #include @@ -47,13 +41,6 @@ float frnd(float range) return (float)rnd(10000)/10000*range; } -struct Spriteset -{ - DWORD *data; - int width; - int height; - int pitch; -}; Spriteset font; Spriteset ld48; @@ -211,51 +198,52 @@ bool LoadSettings(char* filename) FILE* file=fopen(filename, "rb"); if(!file) return false; - + + size_t n; int version=0; - fread(&version, 1, sizeof(int), file); + n = fread(&version, 1, sizeof(int), file); if(version!=100 && version!=101 && version!=102) return false; - fread(&wave_type, 1, sizeof(int), file); + n = fread(&wave_type, 1, sizeof(int), file); sound_vol=0.5f; if(version==102) - fread(&sound_vol, 1, sizeof(float), file); + n = fread(&sound_vol, 1, sizeof(float), file); - fread(&p_base_freq, 1, sizeof(float), file); - fread(&p_freq_limit, 1, sizeof(float), file); - fread(&p_freq_ramp, 1, sizeof(float), file); + n = fread(&p_base_freq, 1, sizeof(float), file); + n = fread(&p_freq_limit, 1, sizeof(float), file); + n = fread(&p_freq_ramp, 1, sizeof(float), file); if(version>=101) - fread(&p_freq_dramp, 1, sizeof(float), file); - fread(&p_duty, 1, sizeof(float), file); - fread(&p_duty_ramp, 1, sizeof(float), file); + n = fread(&p_freq_dramp, 1, sizeof(float), file); + n = fread(&p_duty, 1, sizeof(float), file); + n = fread(&p_duty_ramp, 1, sizeof(float), file); - fread(&p_vib_strength, 1, sizeof(float), file); - fread(&p_vib_speed, 1, sizeof(float), file); - fread(&p_vib_delay, 1, sizeof(float), file); + n = fread(&p_vib_strength, 1, sizeof(float), file); + n = fread(&p_vib_speed, 1, sizeof(float), file); + n = fread(&p_vib_delay, 1, sizeof(float), file); - fread(&p_env_attack, 1, sizeof(float), file); - fread(&p_env_sustain, 1, sizeof(float), file); - fread(&p_env_decay, 1, sizeof(float), file); - fread(&p_env_punch, 1, sizeof(float), file); + n = fread(&p_env_attack, 1, sizeof(float), file); + n = fread(&p_env_sustain, 1, sizeof(float), file); + n = fread(&p_env_decay, 1, sizeof(float), file); + n = fread(&p_env_punch, 1, sizeof(float), file); - fread(&filter_on, 1, sizeof(bool), file); - fread(&p_lpf_resonance, 1, sizeof(float), file); - fread(&p_lpf_freq, 1, sizeof(float), file); - fread(&p_lpf_ramp, 1, sizeof(float), file); - fread(&p_hpf_freq, 1, sizeof(float), file); - fread(&p_hpf_ramp, 1, sizeof(float), file); + n = fread(&filter_on, 1, sizeof(bool), file); + n = fread(&p_lpf_resonance, 1, sizeof(float), file); + n = fread(&p_lpf_freq, 1, sizeof(float), file); + n = fread(&p_lpf_ramp, 1, sizeof(float), file); + n = fread(&p_hpf_freq, 1, sizeof(float), file); + n = fread(&p_hpf_ramp, 1, sizeof(float), file); - fread(&p_pha_offset, 1, sizeof(float), file); - fread(&p_pha_ramp, 1, sizeof(float), file); + n = fread(&p_pha_offset, 1, sizeof(float), file); + n = fread(&p_pha_ramp, 1, sizeof(float), file); - fread(&p_repeat_speed, 1, sizeof(float), file); + n = fread(&p_repeat_speed, 1, sizeof(float), file); if(version>=101) { - fread(&p_arp_speed, 1, sizeof(float), file); - fread(&p_arp_mod, 1, sizeof(float), file); + n = fread(&p_arp_speed, 1, sizeof(float), file); + n = fread(&p_arp_mod, 1, sizeof(float), file); } fclose(file); @@ -687,7 +675,7 @@ bool Slider(int x, int y, float& value, bool bipolar, const char* text) DWORD tcol=0x000000; if(wave_type!=0 && (&value==&p_duty || &value==&p_duty_ramp)) tcol=0x808080; - DrawText(x-4-strlen(text)*8, y+1, tcol, text); + DrawText(font, x-4-strlen(text)*8, y+1, tcol, text); return result; } @@ -714,7 +702,7 @@ bool Button(int x, int y, bool highlight, const char* text, int id) } DrawBar(x-1, y-1, 102, 19, color1); DrawBar(x, y, 100, 17, color2); - DrawText(x+5, y+5, color3, text); + DrawText(font, x+5, y+5, color3, text); if(current && hover && !mouse_left) return true; return false; @@ -743,7 +731,7 @@ bool ButtonWH(int x, int y, int w, int h, bool highlight, const char* text, int } DrawBar(x-1, y-1, w + 2, h + 2, color1); DrawBar(x, y, w, h, color2); - DrawText(x+5, y+5, color3, text); + DrawText(font, x+5, y+5, color3, text); if(current && hover && !mouse_left) return true; return false; @@ -789,7 +777,7 @@ void DrawScreen() ClearScreen(0xC0B090); - DrawText(10, 10, 0x504030, "GENERATOR"); + DrawText(font, 10, 10, 0x504030, "GENERATOR"); for(int i=0;i<7;i++) { if(Button(5, 35+i*30, false, categories[i].name, 300+i)) @@ -959,7 +947,7 @@ void DrawScreen() } } DrawBar(110, 0, 2, 480, 0x000000); - DrawText(120, 10, 0x504030, "MANUAL SETTINGS"); + DrawText(font, 120, 10, 0x504030, "MANUAL SETTINGS"); DrawSprite(ld48, 8, 440, 0, 0xB0A080); @@ -995,7 +983,7 @@ void DrawScreen() if(ButtonWH(490, 140, 17, 17, dragOnLeftClick, "", 101)) dragOnLeftClick = !dragOnLeftClick; } - DrawText(515, 145, 0x000000, "DRAG BARS"); + DrawText(font, 515, 145, 0x000000, "DRAG BARS"); DrawBar(5-1-1, 412-1-1, 102+2, 19+2, 0x000000); if(Button(5, 412, false, "RANDOMIZE", 40)) @@ -1071,7 +1059,7 @@ void DrawScreen() do_play=true; } - DrawText(515, 170, 0x000000, "VOLUME"); + DrawText(font, 515, 170, 0x000000, "VOLUME"); DrawBar(490-1-1+60, 180-1+5, 70, 2, 0x000000); DrawBar(490-1-1+60+68, 180-1+5, 2, 205, 0x000000); DrawBar(490-1-1+60, 180-1, 42+2, 10+2, 0xFF0000); @@ -1083,7 +1071,7 @@ void DrawScreen() if(Button(490, 290, false, "LOAD SOUND", 14)) { char filename[256]; - if(FileSelectorLoad(hWndMain, filename, 1)) // WIN32 + if(FileSelectorLoad(filename, 1)) { ResetParams(); LoadSettings(filename); @@ -1093,7 +1081,7 @@ void DrawScreen() if(Button(490, 320, false, "SAVE SOUND", 15)) { char filename[256]; - if(FileSelectorSave(hWndMain, filename, 1)) // WIN32 + if(FileSelectorSave(filename, 1)) SaveSettings(filename); } @@ -1102,7 +1090,7 @@ void DrawScreen() if(Button(490, 380, false, "EXPORT .WAV", 16)) { char filename[256]; - if(FileSelectorSave(hWndMain, filename, 0)) // WIN32 + if(FileSelectorSave(filename, 0)) ExportWAV(filename); } char str[10]; @@ -1301,9 +1289,9 @@ bool keydown=false; bool ddkCalcFrame() { - input->Update(); // WIN32 (for keyboard input) + input->Update(); // (for keyboard input) - if(input->KeyPressed(DIK_SPACE) || input->KeyPressed(DIK_RETURN)) // WIN32 (keyboard input only for convenience, ok to remove) + if(input->KeyPressed(DIK_SPACE) || input->KeyPressed(DIK_RETURN)) // (keyboard input only for convenience, ok to remove) { if(!keydown) { @@ -1321,7 +1309,7 @@ bool ddkCalcFrame() DrawScreen(); - Sleep(5); // WIN32 + SDL_Delay(5); return true; } @@ -1353,7 +1341,7 @@ void ddkInit() ld48.width=ld48.pitch; - input=new DPInput(hWndMain, hInstanceMain); // WIN32 + input=new DPInput; strcpy(categories[0].name, "PICKUP/COIN"); strcpy(categories[1].name, "LASER/SHOOT"); diff --git a/sfxr/sdlkit.cpp b/sfxr/sdlkit.cpp new file mode 100644 index 0000000..a5d78d0 --- /dev/null +++ b/sfxr/sdlkit.cpp @@ -0,0 +1,422 @@ +/* + Copyright (c) 2007 mjau/GerryJJ + + 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 + AUTHORS OR COPYRIGHT HOLDERS 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. +*/ + +#include "sdlkit.h" + + +#include +#include + +void error (const char *file, unsigned int line, const char *msg) +{ + fprintf(stderr, "[!] %s:%u %s\n", file, line, msg); + exit(1); +} + + +bool keys[SDLK_LAST]; + +bool DPInput::KeyPressed(SDLKey key) +{ + bool r = keys[key]; + keys[key] = false; + return r; +} + +Uint32 *ddkscreen32; +Uint16 *ddkscreen16; +int ddkpitch; +int mouse_x, mouse_y, mouse_px, mouse_py; +bool mouse_left = false, mouse_right = false, mouse_middle = false; +bool mouse_leftclick = false, mouse_rightclick = false, mouse_middleclick = false; + +SDL_Surface *sdlscreen = NULL; + +void sdlupdate () +{ + mouse_px = mouse_x; + mouse_py = mouse_y; + Uint8 buttons = SDL_GetMouseState(&mouse_x, &mouse_y); + bool mouse_left_p = mouse_left; + bool mouse_right_p = mouse_right; + bool mouse_middle_p = mouse_middle; + mouse_left = buttons & SDL_BUTTON(1); + mouse_right = buttons & SDL_BUTTON(3); + mouse_middle = buttons & SDL_BUTTON(2); + mouse_leftclick = mouse_left && !mouse_left_p; + mouse_rightclick = mouse_right && !mouse_right_p; + mouse_middleclick = mouse_middle && !mouse_middle_p; +} + +bool ddkLock () +{ + if(SDL_MUSTLOCK(sdlscreen)) + { + if(SDL_LockSurface(sdlscreen) < 0) + return false; + } + ddkpitch = sdlscreen->pitch / (sdlscreen->format->BitsPerPixel == 32 ? 4 : 2); + ddkscreen16 = (Uint16*)(sdlscreen->pixels); + ddkscreen32 = (Uint32*)(sdlscreen->pixels); + return true; +} + +void ddkUnlock () +{ + if(SDL_MUSTLOCK(sdlscreen)) + { + SDL_UnlockSurface(sdlscreen); + } +} + +void ddkSetMode (int width, int height, int bpp, int refreshrate, int fullscreen, const char *title) +{ + VERIFY(sdlscreen = SDL_SetVideoMode(width, height, bpp, fullscreen ? SDL_FULLSCREEN : 0)); + SDL_WM_SetCaption(title, title); +} + +#include +#include + + +bool Button(int x, int y, bool highlight, const char* text, int id); + +#include +#include + + +#include +#include + +bool ioIsDir(const std::string& filename) +{ + using namespace std; + struct stat status; + stat(filename.c_str(), &status); + + return (status.st_mode & S_IFDIR); +} + +std::list ioList(const std::string& dirname, bool directories, bool files) +{ + using namespace std; + list dirList; + list fileList; + + DIR* dir = opendir(dirname.c_str()); + dirent* entry; + + while ((entry = readdir(dir)) != NULL) + { + #ifdef WIN32 + if(ioIsDir(dirname + "/" + entry->d_name)) + #else + if(entry->d_type == DT_DIR) + #endif + { + if(directories) + dirList.push_back(entry->d_name); + } + else if(files) + fileList.push_back(entry->d_name); + } + + closedir(dir); + + dirList.sort(); + fileList.sort(); + + fileList.splice(fileList.begin(), dirList); + + + return fileList; +} + +extern DPInput *input; + +bool file_select_update() +{ + input->Update(); // (for keyboard input) + + //keydown=false; + + return true; +} + +std::string stoupper(const std::string& s) +{ + std::string result = s; + std::string::iterator i = result.begin(); + std::string::iterator end = result.end(); + + while(i != end) + { + *i = std::toupper((unsigned char)*i); + ++i; + } + return result; +} + + +bool ioExists(const std::string& filename) +{ + return (access(filename.c_str(), 0) == 0); +} + +bool ioNew(const std::string& filename, bool readable, bool writeable) +{ + if(ioExists(filename)) + return false; + + FILE* file = fopen(filename.c_str(), "wb"); + if(file == NULL) + return false; + fclose(file); + return true; +} + + +void ClearScreen(DWORD color); +extern int vcurbutton; + +#include "tools.h" + +extern Spriteset font; + +std::string new_file() +{ + using namespace std; + SDL_EnableUNICODE(1); + SDL_EnableKeyRepeat(0, 0); + + string result; + + bool done = false; + SDL_Event e; + while (!done) + { + while(SDL_PollEvent(&e)) + { + switch (e.type) + { + case SDL_QUIT: + exit(0); + + case SDL_KEYDOWN: + if(e.key.keysym.sym == SDLK_ESCAPE) + { + return ""; + } + if(e.key.keysym.sym == SDLK_RETURN) + { + done = true; + break; + } + + { + char c = e.key.keysym.unicode; + if(0x21 <= c && c <= 0x7E) + result += c; + } + + default: break; + } + } + sdlupdate(); + + ClearScreen(0xC0B090); + + DrawText(font, 90, 150, 0x000000, "TYPE NEW FILE NAME:"); + DrawText(font, 100, 200, 0x000000, "%s", stoupper(result).c_str()); + + SDL_Delay(5); + + SDL_Flip(sdlscreen); + } + + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + SDL_EnableUNICODE(0); + + // FIXME: Force .sfxr + + return result; +} + +void DrawFileSelectScreen(std::list& files, char* buf, bool& gotFile, bool& done) +{ + using namespace std; + + ddkLock(); + + ClearScreen(0xC0B090); + + + int i = 0, j = 0; + for(list::iterator e = files.begin(); e != files.end(); e++) + { + if(40 + 20*i > sdlscreen->h - 50) + { + j++; + i = 0; + } + if(Button(30 + 150*j, 40 + 20*i, false, stoupper(*e).c_str(), 31 + i + j)) + { + gotFile = true; + sprintf(buf, "%s", e->c_str()); + done = true; + } + i++; + } + + if(Button(10, 10, false, "CANCEL", 400)) + { + gotFile = false; + done = true; + } + + if(Button(10, 160, false, "NEW FILE", 401)) + { + string s = new_file(); + if(s != "") + { + ioNew(s, true, true); + files = ioList(".", false, true); + + for(list::iterator e = files.begin(); e != files.end();) + { + if(e->find(".sfxr") == string::npos) + { + e = files.erase(e); + continue; + } + + e++; + } + } + } + + ddkUnlock(); + + if(!mouse_left) + vcurbutton=-1; +} + + +bool select_file (char *buf) +{ + // FIXME: Needs directory browsing + + bool gotFile = false; + using namespace std; + list files; + files = ioList(".", false, true); + + for(list::iterator e = files.begin(); e != files.end();) + { + if(e->find(".sfxr") == string::npos) + { + e = files.erase(e); + continue; + } + + e++; + } + + bool done = false; + SDL_Event e; + while (!done) + { + SDL_PollEvent(&e); + switch (e.type) + { + case SDL_QUIT: + exit(0); + + case SDL_KEYDOWN: + keys[e.key.keysym.sym] = true; + + default: break; + } + sdlupdate(); + + DrawFileSelectScreen(files, buf, gotFile, done); + + SDL_Delay(5); + + SDL_Flip(sdlscreen); + } + return gotFile; +} + +#define FileSelectorLoad(file,y) select_file(file) +#define FileSelectorSave(file,y) select_file(file) + +void sdlquit () +{ + ddkFree(); + SDL_Quit(); +} + +void sdlinit () +{ + SDL_Surface *icon; + VERIFY(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)); + icon = SDL_LoadBMP("/usr/share/sfxr/sfxr.bmp"); + if (!icon) + icon = SDL_LoadBMP("sfxr.bmp"); + if (icon) + SDL_WM_SetIcon(icon, NULL); + atexit(sdlquit); + memset(keys, 0, sizeof(keys)); + ddkInit(); +} + +void loop (void) +{ + SDL_Event e; + while (true) + { + SDL_PollEvent(&e); + switch (e.type) + { + case SDL_QUIT: + exit(0); + + case SDL_KEYDOWN: + keys[e.key.keysym.sym] = true; + + default: break; + } + sdlupdate(); + if (!ddkCalcFrame()) + return; + SDL_Flip(sdlscreen); + } +} + +int main(int argc, char *argv[]) +{ + sdlinit(); + loop(); + return 0; +} + diff --git a/sfxr/sdlkit.h b/sfxr/sdlkit.h index 1d8fd1e..65eb4c3 100644 --- a/sfxr/sdlkit.h +++ b/sfxr/sdlkit.h @@ -24,16 +24,10 @@ #define SDLKIT_H #include "SDL.h" + + #define ERROR(x) error(__FILE__, __LINE__, #x) #define VERIFY(x) do { if (!(x)) ERROR(x); } while (0) -#include -#include - -static void error (const char *file, unsigned int line, const char *msg) -{ - fprintf(stderr, "[!] %s:%u %s\n", file, line, msg); - exit(1); -} typedef Uint32 DWORD; typedef Uint16 WORD; @@ -43,12 +37,18 @@ typedef Uint16 WORD; #define DIK_Z SDLK_z #define DDK_WINDOW 0 -#define hWndMain 0 -#define hInstanceMain 0 -#define Sleep(x) SDL_Delay(x) +extern Uint32 *ddkscreen32; +extern Uint16 *ddkscreen16; +extern int ddkpitch; -static bool keys[SDLK_LAST]; + +extern int mouse_x, mouse_y, mouse_px, mouse_py; +extern bool mouse_left, mouse_right, mouse_middle; +extern bool mouse_leftclick, mouse_rightclick, mouse_middleclick; + + +void error (const char *file, unsigned int line, const char *msg); void ddkInit(); // Will be called on startup bool ddkCalcFrame(); // Will be called every frame, return true to continue running or false to quit @@ -56,149 +56,35 @@ void ddkFree(); // Will be called on shutdown class DPInput { public: - DPInput(int,int) {} + DPInput() {} ~DPInput() {} static void Update () {} - static bool KeyPressed(SDLKey key) - { - bool r = keys[key]; - keys[key] = false; - return r; - } + static bool KeyPressed(SDLKey key); }; -static Uint32 *ddkscreen32; -static Uint16 *ddkscreen16; -static int ddkpitch; -static int mouse_x, mouse_y, mouse_px, mouse_py; -static bool mouse_left = false, mouse_right = false, mouse_middle = false; -static bool mouse_leftclick = false, mouse_rightclick = false, mouse_middleclick = false; +void sdlupdate (); -static SDL_Surface *sdlscreen = NULL; +bool ddkLock (); -static void sdlupdate () -{ - mouse_px = mouse_x; - mouse_py = mouse_y; - Uint8 buttons = SDL_GetMouseState(&mouse_x, &mouse_y); - bool mouse_left_p = mouse_left; - bool mouse_right_p = mouse_right; - bool mouse_middle_p = mouse_middle; - mouse_left = buttons & SDL_BUTTON(1); - mouse_right = buttons & SDL_BUTTON(3); - mouse_middle = buttons & SDL_BUTTON(2); - mouse_leftclick = mouse_left && !mouse_left_p; - mouse_rightclick = mouse_right && !mouse_right_p; - mouse_middleclick = mouse_middle && !mouse_middle_p; -} +void ddkUnlock (); -static bool ddkLock () -{ - if(SDL_MUSTLOCK(sdlscreen)) - { - if(SDL_LockSurface(sdlscreen) < 0) - return false; - } - ddkpitch = sdlscreen->pitch / (sdlscreen->format->BitsPerPixel == 32 ? 4 : 2); - ddkscreen16 = (Uint16*)(sdlscreen->pixels); - ddkscreen32 = (Uint32*)(sdlscreen->pixels); - return true; -} +void ddkSetMode (int width, int height, int bpp, int refreshrate, int fullscreen, const char *title); -static void ddkUnlock () -{ - if(SDL_MUSTLOCK(sdlscreen)) - { - SDL_UnlockSurface(sdlscreen); - } -} -static void ddkSetMode (int width, int height, int bpp, int refreshrate, int fullscreen, const char *title) -{ - VERIFY(sdlscreen = SDL_SetVideoMode(width, height, bpp, fullscreen ? SDL_FULLSCREEN : 0)); - SDL_WM_SetCaption(title, title); -} +//void selected_file (GtkWidget *button, GtkFileSelection *fs); -#include -#include -#include +bool select_file (char *buf); -static char *gtkfilename; +#define FileSelectorLoad(file,y) select_file(file) +#define FileSelectorSave(file,y) select_file(file) -static void selected_file (GtkWidget *button, GtkFileSelection *fs) -{ - strncpy(gtkfilename, gtk_file_selection_get_filename(fs), 255); - gtkfilename[255] = 0; - gtk_widget_destroy(GTK_WIDGET(fs)); - gtk_main_quit(); -} +void sdlquit (); -static bool select_file (char *buf) -{ - gtkfilename = buf; - GtkWidget *dialog = gtk_file_selection_new("It's file selection time!"); - g_signal_connect(G_OBJECT(dialog), "destroy", G_CALLBACK(gtk_main_quit), NULL); - g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), "clicked", G_CALLBACK(selected_file), G_OBJECT(dialog)); - g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button), "clicked", G_CALLBACK(gtk_widget_destroy), G_OBJECT(dialog)); - gtk_widget_show(GTK_WIDGET(dialog)); - gtk_main(); - return *gtkfilename; -} +void sdlinit (); -#define FileSelectorLoad(x,file,y) select_file(file) -#define FileSelectorSave(x,file,y) select_file(file) +void loop (); -static void sdlquit () -{ - ddkFree(); - SDL_Quit(); -} - -static void sdlinit () -{ - SDL_Surface *icon; - VERIFY(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)); - icon = SDL_LoadBMP("/usr/share/sfxr/sfxr.bmp"); - if (!icon) - icon = SDL_LoadBMP("sfxr.bmp"); - if (icon) - SDL_WM_SetIcon(icon, NULL); - atexit(sdlquit); - memset(keys, 0, sizeof(keys)); - ddkInit(); -} - -static void loop (void) -{ - SDL_Event e; - while (true) - { - SDL_PollEvent(&e); - switch (e.type) - { - case SDL_QUIT: - return; - - case SDL_KEYDOWN: - keys[e.key.keysym.sym] = true; - - default: break; - } - sdlupdate(); - if (!ddkCalcFrame()) - return; - SDL_Flip(sdlscreen); - } -} - -int main(int argc, char *argv[]) -{ - gtk_init(&argc, &argv); - sdlinit(); - loop(); - return 0; -} #endif diff --git a/sfxr/tools.cpp b/sfxr/tools.cpp new file mode 100644 index 0000000..49ce024 --- /dev/null +++ b/sfxr/tools.cpp @@ -0,0 +1,161 @@ +/* + Copyright (c) 2007 Tomas Pettersson + + 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 + AUTHORS OR COPYRIGHT HOLDERS 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. +*/ + +#include "sdlkit.h" +#include "tools.h" + +int LoadTGA(Spriteset& tiles, const char *filename) +{ + FILE *file; + unsigned char byte, crap[16], id_length; + int i, width, height, channels, x, y; + file=fopen(filename, "rb"); + if (!file) + return -1; + + int n; + n = fread(&id_length, 1, 1, file); + n = fread(crap, 1, 11, file); + width=0; + height=0; + n = fread(&width, 1, 2, file); // width + n = fread(&height, 1, 2, file); // height + n = fread(&byte, 1, 1, file); // bits + channels=byte/8; + n = fread(&byte, 1, 1, file); // image descriptor byte (per-bit info) + for(i=0;i=0;y--) + for(x=0;x8) + for(x1=0;x1=x && mouse_x=y && mouse_y=0;y--) - for(x=0;x8) - for(x1=0;x1=x && mouse_x=y && mouse_y