Пример #1
0
void graphics_init(graphics_t* g)
{
	sfVideoMode mode = {1024, 768, 32};
	g->render = sfRenderWindow_create(mode, "Vendetta " VERSION, sfResize | sfClose, NULL);
	if (g->render == NULL)
		exit(1);
	sfRenderWindow_setMouseCursorVisible(g->render, sfFalse);
}
Пример #2
0
sfRenderWindow* sfRenderWindow_create_helper
(sfVideoMode* mode, const char* title, sfUint32 style, const sfContextSettings* settings)
{
    return sfRenderWindow_create (*mode, title, style, settings);
}
Пример #3
0
int main(int argc, char **argv) {

	int window_width = 640;
	int window_height = 480;

	//Create the render window
	sfRenderWindow *window = NULL;
	if(!(window = sfRenderWindow_create((sfVideoMode) {window_width, window_height, 32}, "Mettle", sfClose|sfResize, NULL))) {
		printf("Unable to create RenderWindow\n");
		exit(EXIT_FAILURE);
	}

	//Create a queue of inputs
	actionQueue *actions = actionQueue_create();
	playerAction action = actionQueue_next(actions);
	//Load the hero sprite
	sfTexture *hero_texture = NULL;
	static const char *hero_texture_location = "data/img/leatherarmor.png";
	if(!(hero_texture = sfTexture_createFromFile(hero_texture_location, NULL))) {
		printf("Unable to load %s\n", hero_texture_location);
		exit(EXIT_FAILURE);
	}
	entity *hero = entity_createPlayer((sfVector2i) {0, 0}, &action, hero_texture);


	//Create the "map"
	sfTexture *map_texture = NULL;
	static const char *map_texture_location = "data/img/tilesheet.png";
	if(!(map_texture = sfTexture_createFromFile(map_texture_location, NULL))) {
		printf("Unable to load %s\n", map_texture_location);
		exit(EXIT_FAILURE);
	}

	int map_width = (int) ceilf((float) (window_width / TILE_SIZE));
	int map_height = (int) ceilf((float) (window_height / TILE_SIZE));
	//Allocate space
	sfSprite *tile = NULL;
	if(!(tile = sfSprite_create())) {
		printf("Unable to create sprite\n");
		exit(EXIT_FAILURE);
	}
	//Initialize the tile
	sfSprite_setTexture(tile, map_texture, sfFalse);
	sfSprite_setTextureRect(tile, XY_TO_RECT(4, 2, TILE_SIZE, TILE_SIZE));

	//Event holder
	sfEvent event;
	//Window active variable
	sfBool window_active = sfFalse;

	//Main loop
	while(sfRenderWindow_isOpen(window)) {

		//Process events
		while(sfRenderWindow_pollEvent(window, &event)) {
			switch(event.type) {
				//Handle keyboard input
				case(sfEvtKeyPressed):
				case(sfEvtKeyReleased):
					{
						playerAction action = handleKey(&event.key);
						if(action) {
							actionQueue_add(actions, action);
						}
						break;
					}
				case(sfEvtClosed):
					sfRenderWindow_close(window);
					break;
				case(sfEvtGainedFocus):
					window_active = sfTrue;
					break;
				case(sfEvtLostFocus):
					window_active = sfFalse;
				default:
					break;
			}
		}

		//While there are still actions to do
		while(action = actionQueue_next(actions)) {
			hero->ai->think(hero->ai);
		}

		//Clear the screen and render
		sfRenderWindow_clear(window, sfMagenta);

		//Draw the map
		int x,y;
		for(x = 0; x < map_width; x++) {
			for(y = 0; y < map_height; y++) {
				sfSprite_setPosition(tile, (sfVector2f) {x * TILE_SIZE, y * TILE_SIZE});
				sfRenderWindow_drawSprite(window, tile, NULL);
			}
		}

		//Draw the hero
		displayComp_draw(hero->display, window);

		sfRenderWindow_display(window);

		#ifdef DEBUG
		sfVector2f pos = sfSprite_getPosition(hero->display->sprite);
		printf("\rPlayer at %d,%d (%f.0px, %f.0px)", hero->x, hero->y, pos.x, pos.y);
		fflush(stdout);
		#endif
	}

	//Cleanup
	sfRenderWindow_destroy(window);
	return EXIT_SUCCESS;

}
Пример #4
0
void render_main(void) {
    
    // Create the main window and the main view
    window = sfRenderWindow_create(
        (sfVideoMode){DEFAULT_WIN_WIDTH, DEFAULT_WIN_HEIGHT, 32},
        "Territory",
        sfResize | sfClose,
        NULL
        );
        
    if (!window) return;
    
    mainView = sfView_createFromRect(
        (sfFloatRect){0, 0, DEFAULT_WIN_WIDTH, DEFAULT_WIN_HEIGHT});
    sfView_zoom(mainView, 0.75);
    
    /*** ***/
    
    // Create images
    sfImage* oceanImage = sfImage_createFromFile("res/ocean.png");
    if (!oceanImage) return;
    
    sfImage* grassImage = sfImage_createFromFile("res/grass.png");
    if (!grassImage) return;
    
    const int mapWidth = 40;
    const int mapHeight = 20;
    sfImage* mapImage = sfImage_create(mapWidth * 16, mapHeight * 16);
    
    worldmap* map = newMap(mapWidth, mapHeight);
    
    for (int y = 0; y < mapHeight; y++) {
        for (int x = 0; x < mapWidth; x++) {
            sfImage_copyImage(
                mapImage,
                map->tiles[x][y] ? grassImage : oceanImage,
                x * 16,
                y * 16,
                (sfIntRect){0, 0, 16, 16},
                sfTrue);
        }
    }
    
    sfSprite* mapSprite = sfSprite_create();
    sfSprite_setTexture(
        mapSprite,
        sfTexture_createFromImage(mapImage, NULL),
        sfTrue);
        
    /*** ***/
    
    sfVector2u winDimensions;
    render_closeFlag = false;
    eventAvailable = sfFalse;
    while (sfRenderWindow_isOpen(window)) {
        eventAvailable = sfRenderWindow_pollEvent(window, &render_event);
        if (render_closeFlag) {
            render_closeFlag = false;
            sfRenderWindow_close(window);
            sfRenderWindow_destroy(window);
            return;
        }
        if (render_rescaleFlag) {
            render_rescaleFlag = false;
            winDimensions = sfRenderWindow_getSize(window);
            if (winDimensions.x < DEFAULT_WIN_WIDTH) {
                winDimensions.x = DEFAULT_WIN_WIDTH;
                sfRenderWindow_setSize(window, winDimensions);
            }
            if (winDimensions.y < DEFAULT_WIN_HEIGHT) {
                winDimensions.y = DEFAULT_WIN_HEIGHT;
                sfRenderWindow_setSize(window, winDimensions);
            }
            sfView_setSize(
                mainView,
                (sfVector2f){render_event.size.width, render_event.size.height}
                );
            sfView_zoom(mainView, 0.75);
        }
        
        sfRenderWindow_clear(window, sfBlue);
        
        sfRenderWindow_setView(window, mainView);
        sfRenderWindow_drawSprite(window, mapSprite, NULL);
        
        sfRenderWindow_display(window);
        
        Sleep(10);
    }
}
Пример #5
0
int game_setup(jnx_hashmap *configuration)
{
  assert(configuration);
  config = configuration;
  assert(config);
  videomode = sfVideoMode_getDesktopMode();
  JNX_LOG(NULL,"Video mode is %d %d\n",videomode.width,videomode.height);	
  main_window = sfRenderWindow_create(videomode,"Gridfire",sfDefaultStyle,NULL);
  int max_fps = atoi(jnx_hash_get(config,"MAXFPS"));
  assert(max_fps);
  sfRenderWindow_setFramerateLimit(main_window,max_fps);
  main_view = sfView_create();
  sfVector2f view_size;
  view_size.x = videomode.width;
  view_size.y = videomode.height;
  sfView_setSize(main_view,view_size);
  clear_color = sfColor_fromRGB(0,0,0);
  JNX_LOG(NULL,"Creating game _clock\n");
  _clock = sfClock_create();
  JNX_LOG(NULL,"Creating bounding map\n");
  int game_bound = atoi(jnx_hash_get(configuration,"GAMEBOUNDS"));
  cartographer_setbounds(0,game_bound,0,game_bound);

  int star_density = atoi(jnx_hash_get(configuration,"STARCOUNT"));
  starfield_create(cartographer_getbounds(),star_density);
  JNX_LOG(NULL,"Initial setup complete\n");	
  /*-----------------------------------------------------------------------------
   *  Game text
   *-----------------------------------------------------------------------------*/
  game_start_text = game_ui_text_builder("GRIDFIRE",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,40);
  game_over_text = game_ui_text_builder("GAME OVER",sfView_getCenter(main_view),sfColor_fromRGB(255,255,255),sfTextRegular,30);
  game_start_button_text = game_ui_text_builder("Start",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,15);
  game_author_text = game_ui_text_builder("By Alex Jones",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,15);
  next_level_text = game_ui_text_builder("Congratulations",sfView_getCenter(main_view),sfColor_fromRGB(255,255,255),sfTextRegular,45);
  next_level_button_text = game_ui_text_builder("Press enter for the next level",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,20);
  game_loading_text = game_ui_text_builder("LOADING",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,40);
  game_finish_text = game_ui_text_builder("GAME COMPLETE",sfView_getCenter(main_view),sfColor_fromRGB(255,0,0),sfTextRegular,40);
  assert(game_finish_text);
  assert(game_loading_text);
  assert(next_level_button_text);
  assert(game_start_text);
  assert(game_over_text);
  assert(game_start_button_text);
  assert(game_author_text);
  assert(next_level_text);	
  /*-----------------------------------------------------------------------------
   *  Game level setup
   *-----------------------------------------------------------------------------*/
  max_levels = atoi(jnx_hash_get(configuration,"MAXLEVELS"));
  assert(max_levels);
  //start on level one
  current_level = 1;	
  /*-----------------------------------------------------------------------------
   *  Scoreboard setup
   *-----------------------------------------------------------------------------*/
  score_setup(atoi(jnx_hash_get(configuration,"MAXSCORE")));
  /*-----------------------------------------------------------------------------
   *  Load weapon textures
   *-----------------------------------------------------------------------------*/
  weapon_setup();
  /*-----------------------------------------------------------------------------
   *  Load ingame music
   *-----------------------------------------------------------------------------*/
  audio_control_setup(configuration);
  play_music(TITLEMUSIC);
  if(game_ui_setup(main_window,main_view) != 0)
  {
    return 1;
  }
  /*-----------------------------------------------------------------------------
   *  Set game state to running
   *-----------------------------------------------------------------------------*/
  current_game_state = GAMESTART;
  return 0;
}