int bina_init(bina_window_t* window) { /* Init SDL. */ if (sdl_init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "sdl: unable to initialize: %s\n", sdl_get_error()); return BINA_LIB_ERROR; } /* Be verbose in logs. */ sdl_log_set_all_priority(SDL_LOG_PRIORITY_VERBOSE); #ifdef HAVE_STDIO_H /* Set up our own custom log function without userdata. */ sdl_log_set_output_function(log_custom, NULL); #endif if (window) { m.window = sdl_create_window(window->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window->width, window->height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); if (!m.window) { sdl_log_error(SDL_LOG_CATEGORY_APPLICATION, "sdl: unable to create window: %s", sdl_get_error()); return BINA_LIB_ERROR; } } return renderer_init(m.window); }
int main(int argc, char** argv) { int flags = 0; //parse command line arguments int c; while ((c = getopt (argc, argv, "f")) != -1){ switch (c) { case 'f': flags = SDL_WINDOW_FULLSCREEN_DESKTOP; break; default: abort (); } } srand(time(NULL)); sdl_init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); atexit(SDL_Quit); SDL_ShowCursor(SDL_DISABLE); // create a new window SDL_Window* window = sdl_create_window("Tetris simple", &WINDOW_WIDTH, &WINDOW_HEIGHT, flags); //create a renderer SDL_Renderer* renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); //new game game o_game; //init fontmap fontmap o_fontmap; // the 'A' is mapped to a full block to draw shapes const char layout[] = "0123456789A"; // size in pixels for each characters o_fontmap.size = 7; // number of characters in the layout o_fontmap.layout = (char*)&layout; fontmap_init_from_bmp(&o_fontmap, "res/font.bmp", renderer); o_game.p_fontmap = &o_fontmap; game_status ret_code = IN_GAME; while(ret_code != QUIT){ switch(ret_code){ case IN_GAME: ret_code = state_in_game(renderer, &o_game); break; case GAME_OVER: ret_code = state_game_over(renderer, &o_game); break; default: break; } } SDL_Quit(); return EXIT_SUCCESS; }
int main(int argc, char** argv){ //flags for the window int flags = 0; //map default path char *map_load_path = "res/level.map"; char *map_save_path = "res/save.map"; //parse command line arguments int c; while ((c = getopt (argc, argv, "hfm:o:")) != -1){ switch (c) { case 'h': printf("Tilemap editor\n"); printf("h: print this menu and exit\n"); printf("f: launch in fullscreen\n"); printf("m <filename>: map name to load (default %s)\n", map_load_path); printf("o <filename>: file to save to (default %s)\n", map_save_path); return EXIT_SUCCESS; break; case 'f': flags = SDL_WINDOW_FULLSCREEN_DESKTOP; break; case 'm': map_load_path = optarg; break; case 'o': map_save_path = optarg; break; default: abort (); } } sdl_init(SDL_INIT_VIDEO); atexit(SDL_Quit); // create a new window SDL_Window* window = sdl_create_window("Tilemap Editor Isometric Depth", &WINDOW_WIDTH, &WINDOW_HEIGHT, flags); //create a renderer SDL_Renderer* renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); //create a new game game o_game; //init game componants o_game.p_map = map_load(map_load_path, renderer); if(!o_game.p_map){ printf("Cannot load map named '%s'\n", map_load_path); return EXIT_FAILURE; } o_game.map_save_path = map_save_path; o_game.join_mode = false; o_game.tile_mode = TILE; o_game.tile_from_x = -1; o_game.tile_from_y = -1; o_game.tile_from_z = -1; o_game.tile_to_x = -1; o_game.tile_to_y = -1; o_game.tile_to_z = -1; //init fontmap fontmap o_fontmap; const char layout[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!-"; o_fontmap.size = 7; o_fontmap.layout = (char*)&layout; fontmap_init_from_img(&o_fontmap, "res/font.png", renderer); o_game.p_fontmap = &o_fontmap; o_game.p_map->p_fontmap = &o_fontmap; //game states game_status ret_code = IN_GAME; while(ret_code != QUIT){ switch(ret_code){ case IN_GAME: ret_code = state_in_game(renderer, &o_game); break; default: break; } } //free memory map_free(o_game.p_map); fontmap_free(&o_fontmap); SDL_Quit(); return EXIT_SUCCESS; }
int main(void) { /** SDL2 initialized: */ if (sdl_init(SDL_INIT_VIDEO) != 0) { fprintf(stdout, "SDL initialize failed (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Window creation: */ SDL_Window *pWindow = NULL; pWindow = sdl_create_window("Balls", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN); if (pWindow == NULL) { fprintf(stderr, "Windows creation error: (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Renderer creation. */ SDL_Renderer *pRenderer; if ((pRenderer = sdl_create_renderer(pWindow, -1, SDL_RENDERER_ACCELERATED)) == NULL) { fprintf(stderr, "SDL Renderer creating error (%s)\n", SDL_GetError()); exit(EXIT_FAILURE); } /** Random seed initialized. **/ struct timeval tv; gettimeofday(&tv, NULL); srand((unsigned int) tv.tv_usec); /** Define the balls and initialized the balls. **/ Ball *red_ball = init_ball('r'); Ball *green_ball = init_ball('g'); Ball *blue_ball = init_ball('b'); Ball *yellow_ball = init_ball('y'); Ball *pink_ball = init_ball('p'); Ball *turquoise_ball = init_ball('t'); const int FPS = 90; const int FRAME_DELAY = 1000 / FPS; set_loop(true); while (loop) { /** SDL2 mainloop. **/ int frame_start = SDL_GetTicks(); /** Check if a ball collide the border rounded box: **/ check_collide_borders(red_ball); check_collide_borders(green_ball); check_collide_borders(blue_ball); check_collide_borders(yellow_ball); check_collide_borders(pink_ball); check_collide_borders(turquoise_ball); /** Increment the balls positions: **/ translate_ball(red_ball); translate_ball(green_ball); translate_ball(blue_ball); translate_ball(yellow_ball); translate_ball(pink_ball); translate_ball(turquoise_ball); clear(pRenderer, 127, 127, 127, 127); /** Displaying the interior black boxe using a SDL2_gfx function. **/ boxRGBA(pRenderer, 32, 32, WIDTH - 32, HEIGHT - 32, 0, 0, 0, 255); /** Display the balls: **/ display_ball(pRenderer, red_ball); display_ball(pRenderer, green_ball); display_ball(pRenderer, blue_ball); display_ball(pRenderer, yellow_ball); display_ball(pRenderer, pink_ball); display_ball(pRenderer, turquoise_ball); display(pRenderer); update(pWindow); get_events(); int frame_end = SDL_GetTicks() - frame_start; if (frame_end < FRAME_DELAY) { SDL_Delay(FRAME_DELAY - frame_end); } } free_ball(red_ball); free_ball(green_ball); free_ball(blue_ball); free_ball(yellow_ball); free_ball(pink_ball); free_ball(turquoise_ball); clean_up(pRenderer, pWindow); SDL_Quit(); exit(EXIT_SUCCESS); }
static void sdl_out_process(MSFilter *f){ SdlOut *obj=(SdlOut*)f->data; mblk_t *inm0=NULL; mblk_t *inm1=NULL; int err; SDL_Rect smallrect; SDL_Rect rect; bool_t got_preview=FALSE; #if defined(WIN32) || defined(__APPLE__) if (!sdl_initialized){ /* Initialize the SDL library */ if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { ms_error("Couldn't initialize SDL: %s", SDL_GetError()); return; } /* Clean up on exit */ atexit(SDL_Quit); sdl_initialized=TRUE; } #endif if (obj->screen==NULL){ sdl_create_window(obj); } rect.w=obj->size.width; rect.h=obj->size.height; rect.x=0; rect.y=0; smallrect.w=obj->size.width/SCALE_FACTOR; smallrect.h=obj->size.height/SCALE_FACTOR; smallrect.x=obj->size.width - smallrect.w ; smallrect.y=obj->size.height -smallrect.h; while (f->inputs[0]!=NULL && (inm0=ms_queue_get(f->inputs[0]))!=NULL){ SDL_Surface *surf; if (obj->format==MS_YUV420P){ fill_overlay(obj->overlay,inm0); }else { surf=SDL_CreateRGBSurfaceFrom(inm0->b_rptr,obj->size.width,obj->size.height,24,obj->size.width*3,0,0,0,0); err=SDL_BlitSurface(surf,NULL,obj->screen,NULL); if (err<0) ms_warning("Fail to blit surface: %s",SDL_GetError()); SDL_FreeSurface(surf); } freemsg(inm0); } while (f->inputs[1]!=NULL && (inm1=ms_queue_get(f->inputs[1]))!=NULL){ /* this message is blitted on the right,bottom corner of the screen */ SDL_Surface *surf; got_preview=TRUE; if (!obj->lsize_init){ /*attempt to guess the video size of the local preview buffer*/ int bsize=msgdsize(inm1); if (bsize<(MS_VIDEO_SIZE_CIF_W*MS_VIDEO_SIZE_CIF_H*3/2)){ /*surely qcif ?*/ obj->local_size.width=MS_VIDEO_SIZE_QCIF_W; obj->local_size.height=MS_VIDEO_SIZE_QCIF_H; ms_message("preview is in QCIF."); obj->scale_factor=SCALE_FACTOR/2; } obj->lsize_init=TRUE; } if (obj->format==MS_YUV420P){ if (obj->smallb!=NULL) { freemsg(obj->smallb); } obj->smallb=resize_yuv_small(inm1->b_rptr,obj->local_size.width,obj->local_size.height,obj->scale_factor); fill_overlay_at_pos(obj->overlay,obj->smallb,smallrect.x, smallrect.y, smallrect.w, smallrect.h); freemsg(inm1); }else { surf=SDL_CreateRGBSurfaceFrom(inm1->b_rptr,obj->size.width,obj->size.height,24,obj->size.width*3,0,0,0,0); err=SDL_BlitSurface(surf,NULL,obj->screen,&smallrect); if (err<0) ms_warning("Fail to blit surface: %s",SDL_GetError()); SDL_FreeSurface(surf); } } if (!got_preview){ /* this is the case were we have only inm0, we have to redisplay inm1 */ if (obj->format==MS_YUV420P){ if (obj->smallb!=NULL){ fill_overlay_at_pos(obj->overlay,obj->smallb,smallrect.x, smallrect.y, smallrect.w, smallrect.h); } } } if (obj->format==MS_YUV420P) SDL_DisplayYUVOverlay(obj->overlay,&rect); else SDL_UpdateRect(obj->screen,0,0,obj->size.width,obj->size.height); #if defined(WIN32) || defined(__APPLE__) { SDL_Event event; SDL_PollEvent(&event); } #endif }