/* * SDL.flushEvents(min, max) * * Arguments: * min the minimum type event * max the maximum type event */ static int l_event_flushEvents(lua_State *L) { int min = luaL_checkinteger(L, 1); int max = luaL_checkinteger(L, 2); SDL_FlushEvents(min, max); return 0; }
/* * Removes all pending events from SDLs queue. */ void In_FlushQueue(void) { #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); #else SDL_Event event; while(SDL_PollEvent(&event)); #endif Key_MarkAllUp(); }
static mrb_value mrb_sdl2_input_flush_event(mrb_state *mrb, mrb_value self) { mrb_int min, max; int const argc = mrb_get_args(mrb, "i|i", &min, &max); if (1 == argc) { SDL_FlushEvent(min); } else { SDL_FlushEvents(min, max); } return self; }
void TextEntry::GrabFocus() { Screen::SetFocused(this, true); // XXX should this be here? or somewhere else? // In some places (at least the Lua console and the sector view search box), // a keyboard shortcut is used to switch to the text entry widget. // Pressing '`' opens the console, pressing '/' in the sector view selects the search box. // Those are normal text keys, so SDL generates an SDL_TEXTINPUT event for them. // But we don't want to capture that text, because it's not really text input // (it happens "before" the text entry widget gets focus) // So we flush those events from the queue here. SDL_FlushEvents(SDL_TEXTEDITING, SDL_TEXTINPUT); }
static void sdl_input_free(void *data) { sdl_input_t *sdl = (sdl_input_t*)data; if (!data) return; /* Flush out all pending events. */ #ifdef HAVE_SDL2 SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); #else SDL_Event event; while (SDL_PollEvent(&event)); #endif if (sdl->joypad) sdl->joypad->destroy(); free(data); }
char* launchApp(char *appName,int isMenu){ static int nruns = 0; char* tmp = NULL,*script = NULL, *addrBack = NULL; if(appName){ static JSContext *cx = NULL; static JSObject *gl = NULL; script = fileToString(LIBRARY); if(!(cx = JS_NewContext(runtime, 8192))){ fprint(stderr,"Problem creating runtime\n"); exit(EXIT_FAILURE); } JS_SetErrorReporter(cx, reportError); //if(!(gl = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL))){ static struct JSPrincipals def_principles = {0}; if(!(gl = JS_NewGlobalObject(cx, &global_class, NULL))){ fprint(stderr,"Problem creating global object\n"); exit(EXIT_FAILURE); } JSCompartment *cmp = JS_EnterCompartment(cx, gl); if (!(JS_InitStandardClasses(cx, gl))){ fprint(stderr,"Problem creating standard classes\n"); exit(EXIT_FAILURE); } if(!JS_DefineFunctions(cx, gl, jsFunctions)){ fprint(stderr,"Unable to load native functions\n"); exit(EXIT_FAILURE); } globalObject = gl; char *pathToFile = NULL; if(isMenu){ pathToFile = (char*)malloc(1+strlen(MENU_DIR)); strcpy(pathToFile,MENU_DIR); } else { pathToFile = (char*)malloc(strlen(appName) + 1 + strlen(GAMES_DIR)); strcpy(pathToFile,GAMES_DIR); strcat(pathToFile,appName); //strcat(pathToFile,"/"); } JSBool ran = JS_FALSE; jsval retVal; obj.chrootPath = pathToFile; JS_SetPrivate(cx,gl,&obj); SDL_FlushEvents(); //clear queue of events before starting a new app. if(isMenu && 0 == nruns) { //define the property first_run = true; JS_DefineProperty(cx,JS_GetGlobalObject(cx),"first_run",BOOLEAN_TO_JSVAL(JS_TRUE),NULL,NULL,0); } if(script){ loadBaseClasses(appName,pathToFile,cx); //appName is without the extension, appPath needs the directory to chroot ran = JS_EvaluateScript(cx, gl, script, strlen(script) , LIBRARY,0, &retVal); } nruns++; clearModules(cx); JS_GC(JS_GetRuntime(cx)); JS_LeaveCompartment(cx,cmp); //The user requested to quit. if(SDL_QuitRequested()){ if(script)free(script); exitProgram(cx); exit(EXIT_SUCCESS); } if(isMenu && (ran == JS_FALSE)){ if(script) free(script); exitProgram(cx); exit(EXIT_FAILURE); } if(ran != JS_FALSE){ if(JSVAL_IS_STRING(retVal)){ addrBack = JS_EncodeString(cx,JSVAL_TO_STRING(retVal)); tmp = (char*)malloc(strlen(addrBack) +1); strcpy(tmp,addrBack); JS_free(cx,addrBack); addrBack = tmp; } if(JSVAL_IS_BOOLEAN(retVal) && isMenu){ if(script) free(script); if(pathToFile) free(pathToFile); exitProgram(cx); exit(EXIT_SUCCESS); } } else { addrBack = NULL; } if(script) free(script); if(pathToFile) free(pathToFile); JS_DestroyContext(cx); return addrBack; } }
int main (int argc, char* argv[]) { SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_PNG); const std::string IMAGE_DIRECTORY = "C:\\Users\\dK\\Documents\\Visual " \ "Studio 2012\\Projects\\SdlDemoProject\\Debug\\resources\\"; const std::string LINK_IMAGE = IMAGE_DIRECTORY + "KidLink-1.png"; const std::string BACKGROUND = IMAGE_DIRECTORY + "TerribleBackground.png"; Renderer* renderer = new Renderer("SdlDemo", 500, 500, SDL_WINDOW_SHOWN); renderer->clearRenderer(0xFF, 0xFF, 0xFF); Texture* background = new Texture(); AnimatedTexture* linkTexture = loadLink(LINK_IMAGE, renderer->getRenderer()); if (!background->loadFromFile(BACKGROUND, renderer->getRenderer())) { delete linkTexture; delete renderer; IMG_Quit(); SDL_Quit(); return 1; } SDL_Rect blocks[10]; blocks[0].x = 0; blocks[0].y = 0; blocks[1].x = 50; blocks[1].y = 0; blocks[2].x = 0; blocks[2].y = 50; blocks[3].x = 200; blocks[3].y = 200; blocks[4].x = 250; blocks[4].y = 250; blocks[5].x = 450; blocks[5].y = 400; blocks[6].x = 450; blocks[6].y = 450; blocks[7].x = 400; blocks[7].y = 450; blocks[8].x = 450; blocks[8].y = 0; blocks[9].x = 0; blocks[9].y = 450; for (int i = 0; i < 10; i++) { blocks[i].w = 50; blocks[i].h = 50; } SDL_Rect linkLocation = {100, 100, 50, 87}; SDL_Rect oldPosition; SDL_Rect frame = linkTexture->getCurrentFrame()->getDiminsions(); SDL_Rect draw = {0, 0, 500, 500}; SDL_Event events; int xAccelorator = 0; int yAccelorator = 0; while (true) { SDL_PumpEvents(); oldPosition = linkLocation; if (SDL_PeepEvents(&events, 1, SDL_GETEVENT, SDL_QUIT, SDL_KEYUP)) { if (events.type == SDL_QUIT || (events.key.type == SDL_KEYDOWN && events.key.keysym.scancode == SDL_SCANCODE_ESCAPE)) { break; } else if (events.key.type == SDL_KEYDOWN && events.key.keysym.scancode == SDL_SCANCODE_DOWN) { yAccelorator = 5; linkTexture->animate("WalkDown"); } else if (events.key.type == SDL_KEYDOWN && events.key.keysym.scancode == SDL_SCANCODE_UP) { yAccelorator = -5; linkTexture->animate("WalkUp"); } else if (events.key.type == SDL_KEYDOWN && events.key.keysym.scancode == SDL_SCANCODE_RIGHT) { xAccelorator = 5; linkTexture->animate("WalkRight"); } else if (events.key.type == SDL_KEYDOWN && events.key.keysym.scancode == SDL_SCANCODE_LEFT) { xAccelorator = -5; linkTexture->animate("WalkLeft"); } if (events.key.type == SDL_KEYUP) { frame = linkTexture->getIdleFrame()->getDiminsions(); linkTexture->reset(); } else { frame = linkTexture->getCurrentFrame()->getDiminsions(); } linkLocation.x += xAccelorator; linkLocation.y += yAccelorator; if (linkLocation.x < 0 || linkLocation.x + linkLocation.w > 500) { linkLocation = oldPosition; } if (linkLocation.y < 0 || linkLocation.y + linkLocation.h > 500) { linkLocation = oldPosition; } for (int i = 0; i < 10; i++) { if ((blocks[i].x + blocks[i].w >= linkLocation.x && linkLocation.x + linkLocation.w >= blocks[i].x) && (blocks[i].y + blocks[i].h >= linkLocation.y && linkLocation.y + linkLocation.h >= blocks[i].y)) { linkLocation = oldPosition; } } SDL_FlushEvents(SDL_QUIT, SDL_KEYUP); xAccelorator = 0; yAccelorator = 0; } renderer->clearRenderer(0xFF, 0xFF, 0xFF); renderer->drawToRenderer(background->getTexture(), draw); renderer->drawToRenderer(linkTexture->getTexture(), linkLocation, frame); SDL_SetRenderDrawColor(renderer->getRenderer(), 20, 100, 20, 0xFF); for (int i = 0; i < 10; i++) { SDL_RenderFillRect(renderer->getRenderer(), &blocks[i]); } renderer->update(); } delete background; delete linkTexture; delete renderer; IMG_Quit(); SDL_Quit(); return 0; }
void discard_input() { SDL_FlushEvents(INPUT_MIN, INPUT_MAX); }
int Game::run(int screenWidth, int screenHeight, int GAME_SPEED, bool intro){ //Initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ std::cerr<<"SDL_Init failed: "<<SDL_GetError()<<std::endl; return 1; } //Create a Window _window = SDL_CreateWindow("Guns Blood N' Ammo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_OPENGL); if(_window == NULL){ std::cerr<<"Failed to create a SDL_Window: "<<SDL_GetError()<<std::endl; return 2; } _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED); if(_renderer == NULL){ std::cerr<<"Failed to create a SDL_Renderer: "<<SDL_GetError()<<std::endl; return 3; } if(TTF_Init() != 0){ std::cerr<<"TTF_Init failed: "<<SDL_GetError()<<std::endl; return 4; } SDL_ShowCursor(SDL_DISABLE); std::vector<const char*> menuItems, helpItems; menuItems.push_back("Guns Blood N' Ammo"); menuItems.push_back("New Game"); menuItems.push_back("Help"); menuItems.push_back("Options"); menuItems.push_back("Highscore"); menuItems.push_back("Exit"); helpItems.push_back("Help"); helpItems.push_back("WASD to control the player"); helpItems.push_back("Left mouse button to shoot"); helpItems.push_back("Kill all enemies to proceed"); helpItems.push_back("Press Space to return."); std::vector<const char*> optionItems = {"Options", "Crosshair Color: ", "Back"}; std::vector<const char*> highscoreItems = {"Highscore"}; ifstream ifs("highscore.txt"); string time; int counter = 1; while(ifs>>time){ char* output0 = new char[100]; strcpy(output0, std::to_string(counter++).c_str()); strcat(output0, ". "); strcat(output0, time.c_str()); strcat(output0, " s - "); string str; getline(ifs, str, '\n'); strcat(output0, str.c_str()); highscoreItems.push_back(output0); } ifs.close(); _menu = new Menu(menuItems, helpItems, optionItems, highscoreItems); SDL_Event input; char key; const static int MENU = 0; const static int GAMEPLAY = 1; const static int EXIT = 2; const static int GAMEOVER = 3; const static int NEXTLEVEL = 4; const static int WINNING = 5; _mouseL = _mouseR = false; if(intro){ SDL_Surface* image = IMG_Load("GBA.png"); SDL_Texture* image_ = SDL_CreateTextureFromSurface(_renderer, image); SDL_RenderCopy(_renderer, image_, NULL, NULL); SDL_RenderPresent(_renderer); SDL_Delay(2000); SDL_FreeSurface(image); SDL_DestroyTexture(image_); } int gameState = 0; while(gameState != EXIT){ auto fps_start = std::chrono::high_resolution_clock::now(); auto refresh_start = std::chrono::high_resolution_clock::now(); int frames = 0; if(_levels.empty()){ vector<pair<int, int>> enemies = { make_pair(840, 200), make_pair(775, 500), make_pair(320, 800) }; vector<pair<int, int>> enemies2 = { make_pair(16*64, 5*64), make_pair(30*64, 10*64)}; vector<pair<int, int>> enemies3 = {make_pair(16*64, 19*64), make_pair(17*64, 19*64), make_pair(16*64, 20*64), make_pair(17*64, 20*64)}; _levels= {new World(256, 256, enemies, "LEVEL1.TXT", "level1.png"), new World(128,128, enemies2, "LEVEL2.TXT", "level2.png"), new World(480+64,480+64, enemies3, "LEVEL3.TXT", "level3.png")}; _currentLevel = _levels.begin(); } if(gameState == NEXTLEVEL){ ++_currentLevel; if(_currentLevel == _levels.end()){ gameState = WINNING; } else gameState = GAMEPLAY; } while(gameState == MENU){ std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - fps_start; if(elapsed.count() >= 1){ std::cout<<"FPS: "<<frames<<std::endl; frames = 0; fps_start = std::chrono::high_resolution_clock::now(); } else{ frames++; } if(SDL_PollEvent(&input)){ switch(input.type){ case SDL_QUIT: gameState = EXIT; break; case SDL_KEYDOWN: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = true; break; case SDL_KEYUP: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = false; break; } } if(gameState != EXIT) gameState = _menu->input(_keys); elapsed = std::chrono::duration_cast<chrono::milliseconds>(std::chrono::high_resolution_clock::now() - refresh_start); if(elapsed.count()*1000 >= GAME_SPEED/10){ _menu->update(); refresh_start = std::chrono::high_resolution_clock::now(); } _menu->draw(*_renderer); SDL_RenderPresent(_renderer); } while(gameState == GAMEPLAY){ std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - fps_start; if(elapsed.count() >= 1){ _currtime += 1; std::cout<<"FPS: "<<frames<<std::endl; frames = 0; fps_start = std::chrono::high_resolution_clock::now(); } else{ frames++; } while(SDL_PollEvent(&input)){ switch(input.type){ case SDL_QUIT: gameState = EXIT; break; case SDL_KEYDOWN: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = true; break; case SDL_KEYUP: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = false; break; case SDL_MOUSEMOTION: _mouseX = input.motion.x; _mouseY = input.motion.y; break; case SDL_MOUSEBUTTONDOWN: if(input.button.button == SDL_BUTTON_LEFT){ _mouseL = true; } if(input.button.button == SDL_BUTTON_RIGHT){ _mouseR = true; } break; case SDL_MOUSEBUTTONUP: if(input.button.button == SDL_BUTTON_LEFT){ _mouseL = false; } if(input.button.button == SDL_BUTTON_RIGHT){ _mouseR = false; } break; } } if(gameState != EXIT) gameState = (*_currentLevel)->input(_keys, _mouseX, _mouseY, _mouseR, _mouseL); elapsed = std::chrono::duration_cast<chrono::milliseconds>(std::chrono::high_resolution_clock::now() - refresh_start); if(elapsed.count()*1000 >= GAME_SPEED/10){ (*_currentLevel)->update(); refresh_start = std::chrono::high_resolution_clock::now(); } (*_currentLevel)->draw(*_renderer, _currtime); SDL_RenderPresent(_renderer); } if(gameState == GAMEOVER){ SDL_Surface* image = IMG_Load("gameover.png"); SDL_Texture* image_ = SDL_CreateTextureFromSurface(_renderer, image); SDL_RenderCopy(_renderer, image_, NULL, NULL); SDL_RenderPresent(_renderer); SDL_Delay(2000); SDL_FreeSurface(image); SDL_DestroyTexture(image_); _levels.clear(); gameState = 0; } if(gameState == WINNING){ SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255); SDL_RenderClear(_renderer); SDL_Color color = {255, 255, 255, 255}; SDL_Color bgColor = {0, 0, 0, 255}; TTF_Font* font = TTF_OpenFont("BloodLust.ttf", 72); SDL_Surface* surface = NULL; if(!(surface = TTF_RenderText_Shaded(font, "Victory", color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Texture* texture = NULL; texture = SDL_CreateTextureFromSurface(_renderer, surface); SDL_Rect pos = {640/2 - surface->w/2, 480/4, surface->w, surface->h}; SDL_RenderCopy(_renderer, texture, NULL, &pos); SDL_FreeSurface(surface); TTF_CloseFont(font); font = TTF_OpenFont("BloodLust.ttf", 48); char output[100]; strcpy(output,"Time: "); strcat(output,std::to_string(_currtime).c_str()); strcat(output," seconds."); const char* pointer = output; SDL_Surface* text_surface = NULL; SDL_Texture* text_texture = NULL; if(!(text_surface = TTF_RenderText_Shaded(font, pointer, color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } if((text_texture = SDL_CreateTextureFromSurface(_renderer, text_surface)) == NULL){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Rect pos2 = {640/2 - text_surface->w/2, 480/4*2, text_surface->w, text_surface->h}; SDL_RenderCopy(_renderer, text_texture, NULL, &pos2); SDL_FreeSurface(text_surface); vector<pair<int, string>> v; ifstream ifs("highscore.txt"); int i; while(ifs>>i){ string str; getline(ifs, str, '\n'); v.push_back(make_pair(i, str)); } if(_currtime <= v.at(v.size()-1).first){ SDL_StartTextInput(); SDL_Rect pos3 = {0, 0, 256, 32}; string text = ""; bool done = SDL_FALSE; string tecken; char output2[100]; SDL_SetTextInputRect(&pos3); while (!done) { SDL_Event textInputEvent; if (SDL_PollEvent(&textInputEvent)) { switch (textInputEvent.type) { case SDL_QUIT: /* Quit */ done = SDL_TRUE; break; case SDL_TEXTINPUT: /* Add new text onto the end of our text */ tecken.assign(textInputEvent.text.text, textInputEvent.text.text+1); text += tecken; std::cout<<text<<std::endl; break; case SDL_KEYDOWN: if(textInputEvent.key.keysym.sym == SDLK_BACKSPACE){ text = text.substr(0, text.length() - 1); } if(textInputEvent.key.keysym.sym == SDLK_RETURN){ //write to file done = SDL_TRUE; } break; } SDL_RenderClear(_renderer); SDL_RenderCopy(_renderer, texture, NULL, &pos); SDL_RenderCopy(_renderer, text_texture, NULL, &pos2); strcpy(output2,"Name: "); strcat(output2,text.c_str()); const char* pointer2 = output2; SDL_Surface* text_surface2 = NULL; SDL_Texture* text_texture2 = NULL; if(!(text_surface2 = TTF_RenderText_Shaded(font, pointer2, color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } if((text_texture2 = SDL_CreateTextureFromSurface(_renderer, text_surface2)) == NULL){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Rect pos4 = {640/2 - text_surface2->w/2, 480/4*3, text_surface2->w, text_surface2->h}; SDL_RenderCopy(_renderer, text_texture2, NULL, &pos4); SDL_RenderPresent(_renderer); SDL_FreeSurface(text_surface2); SDL_DestroyTexture(text_texture2); } } SDL_StopTextInput(); SDL_DestroyTexture(texture); SDL_DestroyTexture(text_texture); v.at(v.size()-1).first = _currtime; v.at(v.size()-1).second = text; sort(v.begin(), v.end()); ofstream highscore; highscore.open("highscore.txt"); for(auto it = v.cbegin(); it != v.cend(); ++it){ highscore<<it->first<<' '<<it->second<<"\n"; } highscore.close(); _menu->updateHighscore(); } else{ SDL_RenderPresent(_renderer); SDL_Delay(4000); } _levels.clear(); SDL_FlushEvents(SDL_QUIT, SDL_LASTEVENT); gameState = MENU; _currtime = 0; } }
/* * Removes all pending events from SDLs queue. */ void In_FlushQueue(void) { SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); Key_MarkAllUp(); }
// --- void SDLInputHandler::clearPendingEvents () { SDL_FlushEvents (SDL_FIRSTEVENT, SDL_LASTEVENT); }
/** * Open the window and initialize sdl/opengl * @return True if the window opened successfully */ bool System::InitEngine() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) { ReportError("Erreur lors de l'initialisation de la SDL : " + std::string(SDL_GetError())); SDL_Quit(); return false; } else Log("SDL Initialized !"); if (TTF_Init() < 0) { TTF_Quit(); SDL_Quit(); return false; } else Log("SDL_TTF Initialized !"); //Set sdl gl attribute SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24); #ifdef _DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); #endif _mainwindowPtr = SDL_CreateWindow("Rarium", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); _width = 1280; _height = 720; if (!_mainwindowPtr) /* Die if creation failed */ { Log("SDL Window creation failed : " + std::string(SDL_GetError())); TTF_Quit(); SDL_Quit(); return false; } else Log("Window created"); _maincontext = SDL_GL_CreateContext(_mainwindowPtr); SDL_GL_SetSwapInterval(1); //SDL_ShowCursor(SDL_DISABLE); SDL_FlushEvents(SDL_FIRSTEVENT,SDL_LASTEVENT); PluginManager::GetRef().LoadPlugins(); SDL_StartTextInput(); static SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = 1280; rect.h = 720; SDL_SetTextInputRect(&rect); return true; }