int luaExecute(char *f) { int ret = luaL_loadfile(state.lua, f); if (ret == LUA_ERRSYNTAX) { luaHandleError(); return 0; } else if (ret == LUA_ERRMEM) { conAdd(LERR, "Out of memory loading %s", f); return 0; } else if (ret == 0) { // Fall through } else { conAdd(LERR, "Unknown Lua error: %i", ret); return 0; } ret = lua_pcall(state.lua, 0, 0, 0); if (ret == 0) return 1; if (ret == LUA_ERRRUN) { luaHandleError(); return 0; } else if (ret == LUA_ERRMEM) { conAdd(LERR, "Out of memory loading %s", f); return 0; } conAdd(LERR, "Unknown Lua error: %i", ret); return 0; }
int png_save_surface(char *filename, SDL_Surface *surf) { FILE *fp; png_structp png_ptr; png_infop info_ptr; int i, colortype; png_bytep *row_pointers; /* Opening output file */ fp = fopen(filename, "wb"); if (fp == NULL) { conAdd(LERR, "png_save %s : %s", filename, strerror(errno)); return -1; } /* Initializing png structures and callbacks */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, png_user_error, png_user_warn); if (png_ptr == NULL) { conAdd(LERR, "png_save: png_create_write_struct error!"); return -1; } info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { png_destroy_write_struct(&png_ptr, (png_infopp)NULL); conAdd(LERR, "png_save: png_create_info_struct error!"); return -1; } if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return -1; } png_init_io(png_ptr, fp); colortype = png_colortype_from_surface(surf); png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); /* Writing the image */ png_write_info(png_ptr, info_ptr); png_set_packing(png_ptr); row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surf->h); for (i = 0; i < surf->h; i++) row_pointers[i] = (png_bytep)(Uint8 *)surf->pixels + i*surf->pitch; png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, info_ptr); /* Cleaning out... */ free(row_pointers); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return 0; }
void luaHandleError() { conAdd(LNORM, "Stack size: %i", lua_gettop(state.lua)); const char *err = lua_tostring(state.lua, -1); conAdd(LERR, "%s", err); lua_pop(state.lua, 1); }
int luaInit() { luaFree(); state.lua = lua_open(); if (!state.lua) { conAdd(LERR, "Error loading LUA"); return 0; } luaL_openlibs(state.lua); luaopen_base(state.lua); luaopen_table(state.lua); luaopen_string(state.lua); luaopen_math(state.lua); #define AddFunction(a,b) lua_pushcfunction(state.lua, b); lua_setglobal(state.lua, a); AddFunction("particle", luag_spawn) AddFunction("log", luag_log) AddFunction("load", luag_load); return 1; }
int luag_load(lua_State *L) { char *s = (char*)lua_tostring(L, -1); conAdd(1, s); lua_pop(L, 1); s = va("spawn/%s", s); char *f = findFile(s); conAdd(1, f); luaExecute(findFile(f)); return 0; }
void drawFontLetter(float x, float y, int letter) { if ((fonts[letter].id == 0) || (!glIsTexture(fonts[letter].id))) { conAdd(LERR, "texture id %u for character 0x%x is invalid", fonts[letter].id, letter); } glBindTexture(GL_TEXTURE_2D, fonts[letter].id); glCheck(); glPushMatrix(); glTranslatef(x,y,0); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(0,0); glTexCoord2f(0, 1); glVertex2i(0,fonts[letter].h); glTexCoord2f(1, 1); glVertex2i(fonts[letter].w,fonts[letter].h); glTexCoord2f(1, 0); glVertex2i(fonts[letter].w,0); glEnd(); glPopMatrix(); }
int luag_log(lua_State *L) { char *s = (char*)lua_tostring(L, -1); conAdd(1, s); lua_pop(L,1); return 0; }
int isSpawning() { if (state.currentlySpawning) { conAdd(LNORM, "Please wait until all particles are spawned..."); return 1; } return 0; }
int gfxSetResolution() { SDL_VideoInfo* videoInfo; video.screenW = video.screenWtoApply; video.screenH = video.screenHtoApply; if (video.screenAA) { SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4); } video.flags = SDL_OPENGL; if (video.screenFS) video.flags |= SDL_FULLSCREEN; else video.flags |= SDL_RESIZABLE; videoInfo = (SDL_VideoInfo*) SDL_GetVideoInfo(); if (!video.screenW || !video.screenH || video.screenFS) { video.screenW = videoInfo->current_w; video.screenH = videoInfo->current_h; } video.sdlScreen = SDL_SetVideoMode(video.screenW, video.screenH, video.screenBPP, video.flags ); if (!video.sdlScreen) { conAdd(LERR, "SDL_SetVideoMode failed: %s", SDL_GetError()); return 1; } glEnable(GL_TEXTURE_2D); // need to (re)load textures if (!loadFonts()) return 2; if (!loadParticleTexture()) return 3; loadSkyBox(); // not sure if we need to re-attach to new surface //if (video.agarStarted == 1) { // if (AG_SetVideoSurfaceSDL(video.sdlScreen) == -1) { // ( conAdd(LERR, "agar error while attaching to resized window: %s", AG_GetError() ); // } //} return 0; }
void usage() { conAdd(LLOW, GRAVIT_VERSION); conAdd(LLOW, GRAVIT_COPYRIGHT); conAdd(LLOW, ""); conAdd(LLOW, "usage: gravit [-nvh] [COMMAND] ..."); conAdd(LLOW, ""); // ShowHelp("-e, --exec=COMMAND", "execute a command. eg. --exec=\"load foo\"") // ShowHelp("", " commands will execute in order from left to right.") ShowHelp("-n, --noscript", "don't load gravit configuration") ShowHelp("-h, --help", "you're looking at it") ShowHelp("-v, --version", "display version and quit") conAdd(LLOW, ""); conAdd(LLOW, " 'COMMAND' should be encapsulated in quotes when there are spaces involved."); conAdd(LLOW, " For example: gravit spawn record \"save mysim\" \"saveauto 20\""); conAdd(LLOW, ""); cmdQuit(0); }
int loadTexture() { SDL_Surface *tmp = NULL; SDL_Surface *tmp2 = NULL; tmp = SDL_LoadBMP("texture.bmp"); if (!tmp) { conAdd(LNORM, "Failed loading texture"); return 0; } tmp2 = SDL_CreateRGBSurface(SDL_SWSURFACE, 16, 16, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); if (!tmp2) { SDL_FreeSurface(tmp); return 0; } if (SDL_BlitSurface(tmp, NULL, tmp2, NULL)) { SDL_FreeSurface(tmp); SDL_FreeSurface(tmp2); } glGenTextures(1, &texID); glCheck(); glBindTexture(GL_TEXTURE_2D, texID); glCheck(); glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp2->pixels); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glCheck(); SDL_FreeSurface(tmp); SDL_FreeSurface(tmp2); return 1; }
int luag_spawn(lua_State *L) { particle_t *p; particleDetail_t *pd; VectorNew(pos); VectorNew(vel); float mass; int id; VectorZero(pos); VectorZero(vel); mass = lua_tonumber(L, -1); id = -1; lua_pop(L, 1); luag_TableToVector(L, vel); lua_pop(L, 1); luag_TableToVector(L, pos); lua_pop(L, 1); id = lua_tonumber(L, -1); if (id < 0 || id >= state.particleCount) { conAdd(LERR, "Particle %i out of range", id); return 0; } p = getParticleFirstFrame(id); pd = getParticleDetail(id); VectorCopy(pos, p->pos); VectorCopy(vel, p->vel); pd->mass = mass; doVideoUpdateInSpawn(); return 0; }
void drawFontLetter(float x, float y, int letter) { // do not draw blanks and unprintable chars if (letter <= 32) return; if (letter > 127) letter = (int)'?'; // make sure the font letter has a valid texture ID if ((fonts[letter].id == 0) || (!glIsTexture(fonts[letter].id))) { conAdd(LERR, "texture id %u for character 0x%x is invalid", fonts[letter].id, letter); } // draw letter texture glBindTexture(GL_TEXTURE_2D, fonts[letter].id); //glCheck(); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2i(x, y); glTexCoord2f(1, 0); glVertex2i(x+fonts[letter].w, y); glTexCoord2f(0, 1); glVertex2i(x, y+fonts[letter].h); glTexCoord2f(1, 1); glVertex2i(x+fonts[letter].w, y+fonts[letter].h); glEnd(); }
int main(int argc, char *argv[]) { #if !defined(WIN32) && !defined(__MACH__) // disable DGA mouse - it only creates problems, for example: // * SDL < 1.3.x: in fullscreen mode, rotation only works each second click // * VMware mouse driver: rotation goes completely crazy setenv("SDL_VIDEO_X11_DGAMOUSE", "0", 0); #endif if (init(argc, argv)) { conAdd(LERR, "There has been an error on start-up. Read your gravit configration file to possibly fix this."); } else { run(); } clean(); return 0; }
void drawFrame() { particle_t *p; particleDetail_t *pd; int i,j,k; float c; float sc[4]; if (!state.particleHistory) return; switch (view.blendMode) { case 0: glDisable(GL_BLEND); break; case 1: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; case 2: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case 3: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE); break; case 4: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE_MINUS_SRC_ALPHA); break; } if (view.particleRenderMode == 0) { float pointRange[2]; glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 0); glEnable(GL_POINT_SMOOTH); glGetFloatv(GL_POINT_SIZE_RANGE, pointRange); if (view.particleSizeMin < pointRange[0]) { view.particleSizeMin = pointRange[0]; conAdd(LNORM, "Point Size has reached its minimum of %f", view.particleSizeMin); } if (view.particleSizeMin > pointRange[1]) { view.particleSizeMin = pointRange[1]; conAdd(LNORM, "Point Size has reached its maximum of %f", view.particleSizeMin); } glPointSize(view.particleSizeMin); } if (view.particleRenderMode == 1) { float quadratic[] = { 0.0f, 0.0f, 0.01f }; if (!video.supportPointParameters || !video.supportPointSprite) { conAdd(LNORM, "Sorry, Your video card does not support GL_ARB_point_parameters and/or GL_ARB_point_sprite."); conAdd(LNORM, "This means you can't have really pretty looking particles."); conAdd(LNORM, "Setting particleRenderMode to 2"); view.particleRenderMode = 2; return; } glDisable(GL_DEPTH_TEST); glDisable(GL_POINT_SMOOTH); // enabling this makes particles dissapear glPointParameterfvARB_ptr( GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic ); glPointParameterfARB_ptr( GL_POINT_SIZE_MAX_ARB, view.particleSizeMax ); glPointParameterfARB_ptr( GL_POINT_SIZE_MIN_ARB, view.particleSizeMin ); glPointSize( view.particleSizeMax ); // lets you put textures on the sprite // doesn't work on some cards for some reason :( // so i had to make textures an option with this mode if (view.particleRenderTexture) { glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE ); glBindTexture(GL_TEXTURE_2D, particleTextureID); } else { glBindTexture(GL_TEXTURE_2D, 0); } glEnable( GL_POINT_SPRITE_ARB ); } else if (view.particleRenderMode == 2) { glDisable(GL_DEPTH_TEST); if (view.particleRenderTexture) { glBindTexture(GL_TEXTURE_2D, particleTextureID); } else { glBindTexture(GL_TEXTURE_2D, 0); } } if (view.particleRenderMode == 0 || view.particleRenderMode == 1) { glBegin(GL_POINTS); for (i = 0; i < state.particleCount; i++) { VectorNew(pos); pd = state.particleDetail + i; glColor4fv(pd->col); if (view.frameSkip < 0) { particleInterpolate(i, ((float)view.frameSkipCounter / view.frameSkip), pos); glVertex3fv(pos); } else { p = state.particleHistory + state.particleCount * state.currentFrame + i; glVertex3fv(p->pos); } view.vertices++; } glEnd(); } else if (view.particleRenderMode == 2) { // my math mojo is not so great, so this may not be the most efficient way of doing this // this whole bit is dodgy too, as long as it works :) GLdouble matProject[16]; GLdouble matModelView[16]; GLint viewport[4]; GLdouble screen[3]; glMatrixMode(GL_PROJECTION); glPushMatrix(); glCheck(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glCheck(); glGetDoublev(GL_PROJECTION_MATRIX, matProject); glCheck(); glGetDoublev(GL_MODELVIEW_MATRIX, matModelView); glCheck(); glGetIntegerv(GL_VIEWPORT, viewport); glCheck(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (view.stereoMode == 1) { if (view.stereoModeCurrentBit == 0) { glOrtho(0.0,video.screenW/2,0,video.screenH,-1.0,1.0); } else { glOrtho(video.screenW/2,video.screenW,0,video.screenH,-1.0,1.0); } } else { glOrtho(0.0f,video.screenW,0,video.screenH,-1.0f,1.0f); } glCheck(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCheck(); for (i = 0; i < state.particleCount; i++) { double size; VectorNew(moo); float *pos; int success; pos = moo; pd = state.particleDetail + i; if (view.frameSkip < 0) { particleInterpolate(i, ((float)view.frameSkipCounter / view.frameSkip), moo); } else { p = state.particleHistory + state.particleCount * state.currentFrame + i; pos = p->pos; } success = gluProject( pos[0],pos[1],pos[2], matModelView, matProject, viewport, &screen[0], &screen[1], &screen[2] ); if ((success != GL_TRUE) || (screen[2] > 1)) continue; size = view.particleSizeMin + (1.f - (float)screen[2]) * view.particleSizeMax; glBegin(GL_QUADS); glColor4fv(pd->col); glTexCoord2i(0,0); glVertex2d(screen[0]-size, screen[1]-size); glTexCoord2i(1,0); glVertex2d(screen[0]+size, screen[1]-size); glTexCoord2i(1,1); glVertex2d(screen[0]+size, screen[1]+size); glTexCoord2i(0,1); glVertex2d(screen[0]-size, screen[1]+size); glEnd(); view.vertices += 4; } glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); } if (view.particleRenderMode == 1 && video.supportPointParameters && video.supportPointSprite) { glDisable( GL_POINT_SPRITE_ARB ); } glBindTexture(GL_TEXTURE_2D, 0); // sc[3] = 1; if (view.tailLength > 0 || view.tailLength == -1) { // Not sure why this helps but, // it is a fix for one case where only points are drawn instead of lines // on radeon 9600 after switching to particlerendermode 0 from 1 if (view.particleRenderMode == 0) glLineWidth(view.tailWidth+1); glLineWidth(view.tailWidth); for (i = 0; i < state.particleCount; i++) { int to; p = 0; glBegin(GL_LINE_STRIP); if (view.tailLength == -1) k = 0; else if (state.currentFrame < (view.tailLength+2)) k = 0; else k = state.currentFrame - (view.tailLength+2); if (state.mode & SM_RECORD) to = state.currentFrame; else to = state.currentFrame + 1; for (j = k; j <= state.currentFrame; j+=view.tailSkip ) { //for (j = state.currentFrame; j >= k; j-=view.tailSkip ) { if (j >= state.historyFrames) continue; pd = state.particleDetail + i; if (view.tailFaded) c = (float)(j-k) / (float)(state.currentFrame-k) * view.tailOpacity; else c = view.tailOpacity; memcpy(sc, pd->col, sizeof(float)*4); sc[3] *= c; glColor4fv(sc); p = state.particleHistory + state.particleCount * j + i; glVertex3fv(p->pos); view.vertices++; } if (view.frameSkip < 0 && !(state.mode & SM_RECORD)) { VectorNew(pos); particleInterpolate(i, ((float)view.frameSkipCounter / view.frameSkip), pos); glVertex3fv(pos); } else { p = state.particleHistory + state.particleCount * state.currentFrame + i; glVertex3fv(p->pos); } glEnd(); } } }
int gfxInit() { int detectedBPP; SDL_Surface *icon; int ret; char *fileName; if (SDL_Init(SDL_INIT_VIDEO)) { conAdd(LERR, "SDL Init failed"); conAdd(LERR, SDL_GetError()); sdlCheck(); return 0; } if (TTF_Init()) { conAdd(LERR, "SDL_ttf Init failed"); conAdd(LERR, SDL_GetError()); sdlCheck(); return 0; } video.sdlStarted = 1; fileName = findFile(MISCDIR "/gravit.png"); if (!fileName) { return 0; } icon = IMG_Load(fileName); if (!icon) { sdlCheck(); } SDL_WM_SetIcon(icon, NULL); SDL_FreeSurface(icon); setTitle(0); video.gfxInfo = (SDL_VideoInfo*) SDL_GetVideoInfo(); detectedBPP = video.gfxInfo->vfmt->BitsPerPixel; conAdd(LLOW, "Detected %i BPP", detectedBPP); gfxInitRetry: if (video.screenBPP == 0) video.screenBPP = detectedBPP; ret = gfxSetResolution(); if (ret) { if (ret == 1) { if (video.screenAA) { conAdd(LERR, "You have videoantialiasing on. I'm turning it off and restarting..."); video.screenAA = 0; goto gfxInitRetry; } if (detectedBPP != video.screenBPP) { conAdd(LERR, "Your BPP setting is different to your desktop BPP. I'm restarting with your desktop BPP..."); video.screenBPP = detectedBPP; goto gfxInitRetry; } } return 0; } conAdd(LLOW, "Your video mode is %ix%ix%i", video.screenW, video.screenH, video.gfxInfo->vfmt->BitsPerPixel ); if (!video.screenAA && view.particleRenderMode == 1) { conAdd(LERR, "Warning! You don't have videoantialiasing set to 1. From what I've seen so far"); conAdd(LERR, "this might cause particlerendermode 1 not to work. If you don't see any particles"); conAdd(LERR, "after spawning, hit the \\ (backslash) key)."); } glClearColor(0, 0, 0, 0); glShadeModel(GL_SMOOTH); glEnable(GL_LINE_SMOOTH); glDisable(GL_DEPTH_TEST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glCheck(); checkPointParameters(); checkPointSprite(); checkDriverBlacklist(); SDL_ShowCursor(view.showCursor); SDL_EnableUNICODE(SDL_ENABLE); SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL); #ifndef WITHOUT_AGAR AG_InitCore("gravit", 0); //AG_InitGraphics("sdlgl"); if (AG_InitVideoSDL(video.sdlScreen, AG_VIDEO_OVERLAY | AG_VIDEO_OPENGL_OR_SDL) == -1) conAdd(LERR, "agar error while initializing main window: %s", AG_GetError() ); video.agarStarted = 1; if (!view.screenSaver) osdInitDefaultWindows(); #endif return 1; }
void png_user_error(png_structp ctx, png_const_charp str) { conAdd(LERR, "libpng error: %s", str); }
int loadFonts() { TTF_Font *font = NULL; SDL_Surface *fontSurface = NULL; SDL_Surface *tmp = NULL; static SDL_Color fontColour = {255,255,255}; char letter[2]; int i; char *p; letter[1] = 0; p = va("%s/%s", MISCDIR, video.fontFile); if (!fileExists(p)) { conAdd(LERR, "Could not open %s", p); return 0; } font = TTF_OpenFont(p, video.fontSize); if (!font) { conAdd(LERR, "Could not open %s", p); return 0; } TTF_SetFontStyle(font, TTF_STYLE_NORMAL); memset(fonts, 0, sizeof(fonts)); fontHeight = 0; for (i = 32; i < 128; i++) { letter[0] = i; fontSurface = TTF_RenderText_Solid(font, letter, fontColour); if (!fontSurface) { TTF_CloseFont(font); return 0; } fonts[i].ow = fontSurface->w; fonts[i].oh = fontSurface->h; if (fonts[i].oh > fontHeight) fontHeight = (float)fonts[i].oh; fonts[i].w = gfxPowerOfTwo(fonts[i].ow); fonts[i].h = gfxPowerOfTwo(fonts[i].oh); if (fonts[i].w > fonts[i].h) fonts[i].h = fonts[i].w; if (fonts[i].h > fonts[i].w) fonts[i].w = fonts[i].h; tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, fonts[i].w, fonts[i].h, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); if (!tmp) { TTF_CloseFont(font); SDL_FreeSurface(fontSurface); return 0; } if (SDL_BlitSurface(fontSurface, NULL, tmp, NULL)) { TTF_CloseFont(font); SDL_FreeSurface(tmp); SDL_FreeSurface(fontSurface); return 0; } glGenTextures(1, &fonts[i].id); glCheck(); glBindTexture(GL_TEXTURE_2D, fonts[i].id); glCheck(); glTexImage2D(GL_TEXTURE_2D, 0, 3, fonts[i].w, fonts[i].h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp->pixels); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glCheck(); SDL_FreeSurface(tmp); SDL_FreeSurface(fontSurface); } TTF_CloseFont(font); return 1; }
int commandLineRead(int argc, char *argv[]) { int i; // char *parm; // char *argvptr; #define CheckCommand(x) !strncmp(argv[i], x, strlen(x)) for (i = 1; i < argc; i++) { // version, quit if (CheckCommand("--version") || CheckCommand("-v")) { conAdd(LLOW, GRAVIT_VERSION); conAdd(LLOW, GRAVIT_COPYRIGHT); cmdQuit(0); return 0; } // --help or -h if (CheckCommand("--help") || CheckCommand("-h")) { usage(); return 0; } // -n or --noscript if (CheckCommand("--noscript") || CheckCommand("-n")) { state.dontExecuteDefaultScript = 1; continue; } #ifdef WIN32SCREENSAVER if (CheckCommand("/S") || CheckCommand("/s")/* || CheckCommand("/P") || CheckCommand("/p")*/) { char *path; path = getRegistryString(REGISTRY_NAME_PATH); if (!path || strlen(path) == 0) { MessageBox(NULL, "Can't work out where Gravit lives!\nTry running Gravit by itself first", "Gravit Screensaver Error", MB_OK); cmdQuit(0); return 0; } SetCurrentDirectory(path); configRead(findFile(SCREENSAVER_FILE), 0); state.dontExecuteDefaultScript = 1; view.screenSaver = 1; continue; } // just ignore preview mode, configure dialog and change password if (CheckCommand("/P") || CheckCommand("/p") || CheckCommand("/C") || CheckCommand("/c") || CheckCommand("/A") || CheckCommand("/a")) { cmdQuit(0); return 0; } #endif cmdExecute(argv[i]); } return 1; }
void png_user_warn(png_structp ctx, png_const_charp str) { conAdd(LNORM, "libpng warning: %s", str); }
void conInput(SDLKey c) { if (c >= 32 && c < 127) { if (c == SDLK_SPACE && conCommandPos == 0) return; if (conCommandPos < CONSOLE_LENGTH) { int i,l; l = strlen(conCommand); for (i = strlen(conCommand); i > conCommandPos-1; i--) conCommand[i+1]=conCommand[i]; conCommand[conCommandPos] = c; conCommandPos++; conCompWord[0] = 0; } return; } if (c == SDLK_BACKSPACE || c == SDLK_DELETE) { if (conCommandPos > 0) { strcpy(&conCommand[conCommandPos-1], &conCommand[conCommandPos]); conCommandPos--; } conCompWord[0] = 0; return; } if (c == SDLK_RETURN || c == 10) { printf("\n\r"); if (conCommandPos == 0) { view.consoleMode = 0; return; } // add command to typed history conTypedHistoryAdd(conCommand); view.consoleMode = 0; cmdExecute(conCommand); conCommand[0] = 0; conCommandPos = 0; conCompWord[0] = 0; return; } if (c == SDLK_BACKQUOTE || c == SDLK_ESCAPE) { view.consoleMode = 0; conCommandPos = 0; conCommand[0] = 0; conCompWord[0] = 0; return; } if (c == SDLK_TAB) { conAutoComplete(); return; } if (c == SDLK_UP) { conTypedHistoryChange(-1); return; } if (c == SDLK_DOWN) { conTypedHistoryChange(1); return; } if (c == SDLK_LEFT) { if (conCommandPos > 0) { conCommandPos--; } return; } if (c == SDLK_RIGHT) { if (conCommandPos < (signed)strlen(conCommand)) { conCommandPos++; } return; } conAdd(LLOW, "unknown key: %u", c); }
void conAutoComplete() { int lenCommand; int lenString; int i, j; cmd_t *c; if (!strlen(conCommand)) return; // see if there is an tab complete in progress if (strlen(conCompWord) == 0 || strncmp(conCompWord, conCommand, strlen(conCompWord)) != 0) { // new tab complete, lets search for words conCompWordsFoundCount = 0; conCompWord[0] = 0; lenString = strlen(conCommand); i = -1; while (1) { i++; c = &cmd[i]; if (!c->cmd) break; lenCommand = strlen(c->cmd); if (lenString > lenCommand) { continue; } if (!strncmp(conCommand, c->cmd, lenString)) { conAdd(LNORM, c->cmd); // this is the first command found, so lets save it as the most common word if (!conCompWordsFoundCount) { strcpy(conCompWord, c->cmd); // see what we can match } else { int l; l = strlen(conCompWord); if (lenCommand < l) l = lenCommand; for (j = 0; j < l; j++) { if (conCompWord[j] != c->cmd[j]) break; } conCompWord[j] = 0; } if (conCompWordsFoundCount < MAX_COMPLETE_LIST) { conCompWordsFoundPtrs[conCompWordsFoundCount] = c->cmd; conCompWordsFoundCount++; } else { break; } } } conCompWordsFoundIndex = -1; } if (!conCompWordsFoundCount) { conAdd(LERR, "No commands starting with %s", conCommand); return; } conCompWordsFoundIndex++; if (conCompWordsFoundIndex >= conCompWordsFoundCount) conCompWordsFoundIndex = 0; strcpy(conCommand, conCompWordsFoundPtrs[conCompWordsFoundIndex]); conCommandPos = strlen(conCommand); }
int pickPositions() { int gals; VectorNew(galPos[100]); VectorNew(galVel[100]); VectorNew(shit); float galSize[100]; float galMassMin[100]; float galMassMax[100]; float spawnRange; int i; int g; particle_t *p; particleDetail_t *pd; float totalMass = 0; float angle; float angle2; float radius; gals = (rand() % (1 + spawnVars.maxGalCount-spawnVars.minGalCount)) + spawnVars.minGalCount; if (gals <= 0) { conAdd(LERR, "For some reason galaxies to spawn is 0 or less. Not possible!"); return 0; } if (gals >= 100) { conAdd(LERR, "Maximum galaxies to spawn is 100"); return 0; } spawnRange = frand(spawnVars.minSpawnRange, spawnVars.maxSpawnRange); conAdd(LNORM, "Spawning new simulation..."); conAdd(LLOW, "- %i particles...", state.particleCount); conAdd(LLOW, "- %i galaxies...", gals); for (g = 0; g < gals; g++) { galMassMin[g] = frand(spawnVars.minGalMass, spawnVars.maxGalMass); galMassMax[g] = frand(spawnVars.minGalMass, spawnVars.maxGalMass); galSize[g] = frand(spawnVars.minGalSize, spawnVars.maxGalSize); setRangePosition(galPos[g], spawnRange); setRangePosition(galVel[g], frand(0,1) * frand(0,1) * frand(spawnVars.minGalVel, spawnVars.maxGalVel)); } for (i = 0; i < state.particleCount; i++) { if (!(i % 100)) { view.recordParticlesDone = i; doVideoUpdateInSpawn(); } if (state.restartSpawning) { return 0; } p = getParticleFirstFrame(i); pd = getParticleDetail(i); g = rand() % gals; pd->mass = frand(galMassMin[g], galMassMax[g]); // if (g % 2 == 0) // pd->mass = -pd->mass; totalMass += pd->mass; // position VectorCopy(galPos[g], p->pos); setRangePosition((float *)&shit, galSize[g]); VectorAdd(p->pos, shit, p->pos); // galaxy structured position angle = frand(0, PI*2); radius = frand(0, galSize[g]); VectorZero(p->pos); p->pos[0] = cos(angle) * radius; p->pos[1] = sin(angle) * radius; p->pos[2] = frand(-radius/10, radius/10); VectorAdd(galPos[g], p->pos, p->pos); angle2 = angle + PI / 2; p->vel[0] = cos(angle2) * radius * 0.05f; p->vel[1] = sin(angle2) * radius * 0.05f; p->vel[2] = 0; VectorAdd(galVel[g], p->vel, p->vel); if (g & 2) { p->vel[0] = -p->vel[0]; p->vel[1] = -p->vel[1]; p->vel[2] = -p->vel[2]; } } conAdd(LLOW, "- %f total mass...", totalMass); conAdd(LLOW, "- %f galaxy mass...", totalMass / gals); conAdd(LLOW, "- %f particle mass...", totalMass / state.particleCount); return 0; }
int loadFonts() { TTF_Font *font = NULL; SDL_Surface *fontSurface = NULL; SDL_Surface *tmp = NULL; Uint32 saved_flags; Uint8 saved_alpha; static SDL_Color fontColour = {255,255,255}; char letter[2]; int i; char *p; letter[1] = 0; p = findFile(va("%s/%s", MISCDIR, video.fontFile)); if (!p) { conAdd(LERR, "Could not open font"); return 0; } font = TTF_OpenFont(p, video.fontSize); if (!font) { conAdd(LERR, "Could not open %s", p); return 0; } TTF_SetFontStyle(font, TTF_STYLE_NORMAL); #ifdef TTF_HINTING_NORMAL TTF_SetFontHinting(font, TTF_HINTING_NORMAL); #endif memset(fonts, 0, sizeof(fonts)); fontHeight = 0; for (i = 32; i < 128; i++) { letter[0] = i; fontSurface = TTF_RenderText_Blended(font, letter, fontColour); if (!fontSurface) { #ifdef WIN32 MessageBox(NULL, TTF_GetError(), "gravit: font engine error", MB_OK); #else printf("%s\n", TTF_GetError()); #endif TTF_CloseFont(font); return 0; } fonts[i].ow = fontSurface->w; fonts[i].oh = fontSurface->h; if (fonts[i].oh > fontHeight) fontHeight = (float)fonts[i].oh; fonts[i].w = gfxPowerOfTwo(fonts[i].ow); fonts[i].h = gfxPowerOfTwo(fonts[i].oh); if (fonts[i].w > fonts[i].h) fonts[i].h = fonts[i].w; if (fonts[i].h > fonts[i].w) fonts[i].w = fonts[i].h; tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, fonts[i].w, fonts[i].h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks depend on byteorder */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); #endif if (!tmp) { TTF_CloseFont(font); SDL_FreeSurface(fontSurface); return 0; } /* Save the alpha blending attributes */ saved_flags = fontSurface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK); #if SDL_VERSION_ATLEAST(1, 3, 0) SDL_GetSurfaceAlphaMod(fontSurface, &saved_alpha); SDL_SetSurfaceAlphaMod(fontSurface, 0xFF); #else saved_alpha = fontSurface->format->alpha; if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(fontSurface, 0, 0); } #endif /* copy to texture surface */ if (SDL_BlitSurface(fontSurface, NULL, tmp, NULL)) { TTF_CloseFont(font); SDL_FreeSurface(tmp); SDL_FreeSurface(fontSurface); return 0; } glGenTextures(1, &fonts[i].id); glCheck(); glBindTexture(GL_TEXTURE_2D, fonts[i].id); glCheck(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fonts[i].w, fonts[i].h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp->pixels); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glCheck(); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glCheck(); SDL_FreeSurface(tmp); SDL_FreeSurface(fontSurface); } TTF_CloseFont(font); return 1; }
int init(int argc, char *argv[]) { #if WIN32 char currentDirectory[MAX_PATH]; #endif srand(time(0)); conInit(); loadDefaults(); timerInit(); if (!commandLineRead(argc, argv)) return 0; if (view.quit) return 0; if (!state.dontExecuteDefaultScript) configRead(findFile(CONFIG_FILE), 0); #ifndef NO_GUI if (!gfxInit()) return 1; #endif if (state.historyFrames % 2) state.historyFrames--; fpsInit(); #ifdef WIN32 // if we've gone this far, lets set the registry key even if it exists... GetCurrentDirectory(MAX_PATH, currentDirectory); setRegistryString(REGISTRY_NAME_PATH, currentDirectory); #endif #ifdef _OPENMP conAdd(LHELP, "multi-threaded rendering: max threads = %d. Found %d processors.", state.processFrameThreads, omp_get_num_procs()); #endif #ifndef NO_STDIO_REDIRECT // say hi (and keep stdout.txt alive on windows...) if(!view.useStdout && !view.screenSaver) printf("Welcome to %s.\n", GRAVIT_VERSION); #endif #ifdef WITHOUT_AGAR conAdd(LHELP, "Welcome to Gravit!"); #ifndef NO_GUI conAdd(LHELP, "Quick Start: Hit SPACE to start a new simulation!"); conAdd(LHELP, "Hold down a mouse button and move it around. Use A and Z keys, or the scroll wheel to zoom in and out."); #endif #endif return 0; }