Exemplo n.º 1
0
Arquivo: view.c Projeto: jep-dev/spide
void View_draw(sfRenderWindow *win, int ticks) {
	sfRenderWindow_setActive(win, true);
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(0,0,0, 0,1,0, 0,0,1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glColor3f(1.0,1.0,1.0);
	glBegin(GL_QUADS);
	float w=.5, h=.5;
	glVertex3f(w,0,-h);
	glVertex3f(-w,0,-h);
	glVertex3f(-w,0,h);
	glVertex3f(w,0,h);
	glEnd();
	
	glTranslatef(0,-2,0);
	sfRenderWindow_display(win);
}
Exemplo n.º 2
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;

}
Exemplo n.º 3
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);
    }
}
Exemplo n.º 4
0
void game_run()
{
  JNX_LOG(NULL,"Starting run loop\n");
  sfEvent current_event;
  sfTime time;
  float current_time;
  while(sfRenderWindow_isOpen(main_window))
  {
    switch(current_game_state)
    {
      case COMPLETE:
        sfRenderWindow_clear(main_window,clear_color);	
        sfRenderWindow_pollEvent(main_window,&current_event);
        switch(current_event.key.code)
        {
          case sfKeyReturn:
            sfRenderWindow_close(main_window);
        }
        sfVector2f completepos = sfView_getCenter(main_view);
        int string_length = strlen(sfText_getString(game_finish_text));
        int complete_offset = sfText_getCharacterSize(game_finish_text) * string_length;
        completepos.x = completepos.x - (complete_offset / 2);
        sfText_setPosition(game_finish_text,completepos);

        sfRenderWindow_drawText(main_window,game_finish_text,NULL);
        sfRenderWindow_display(main_window);	
        break;
      case NEXT_LEVEL:
        sfRenderWindow_clear(main_window,clear_color);	
        sfRenderWindow_pollEvent(main_window,&current_event);
        switch(current_event.key.code)
        {
          case sfKeyEscape:
            sfRenderWindow_close(main_window);
            break;
          case sfKeyReturn:
            game_setup_next_level();
            break;
        }
        sfVector2f nlpos = sfView_getCenter(main_view);
        int nltext_offset = strlen(sfText_getString(next_level_text));
        nltext_offset = nltext_offset * sfText_getCharacterSize(next_level_text);
        sfVector2f nlnewpos;
        nlnewpos.x	= nlpos.x - (nltext_offset / 2);
        nlnewpos.y = nlpos.y;
        sfText_setPosition(next_level_text,nlnewpos);
        int nlbt_offset = strlen(sfText_getString(next_level_button_text));
        nlbt_offset = nlbt_offset * sfText_getCharacterSize(next_level_button_text);
        nlnewpos.x = nlpos.x - (nlbt_offset /2);
        nlnewpos.y = nlnewpos.y + 50;
        sfText_setPosition(next_level_button_text,nlnewpos);
        switch(text_yellow)
        {
          case 0:
            sfText_setColor(next_level_button_text,sfColor_fromRGB(255,255,0));
            text_yellow = 1;
            break;
          case 1:
            sfText_setColor(next_level_button_text,sfColor_fromRGB(255,0,0));
            text_yellow = 0;
            break;
        }
        sfRenderWindow_drawText(main_window,next_level_text,NULL);
        sfRenderWindow_drawText(main_window,next_level_button_text,NULL);
        sfRenderWindow_display(main_window);	
        break;
      case RUNNING:
        time = sfClock_getElapsedTime(_clock);
        current_time = sfTime_asSeconds(time);
        sfRenderWindow_pollEvent(main_window,&current_event);
        /*-----------------------------------------------------------------------------
         *  Slowly zoom in as we start the level
         *-----------------------------------------------------------------------------*/
        /*  	if(sfView_getSize(main_view).x > videomode.width)
                {
                sfView_zoom(main_view,0.99f);
                }
                */
        switch(current_event.key.code)
        {
          case sfKeyEscape:
            sfRenderWindow_close(main_window);
            break;
        }
        if(score_max_achieved() == 1)
        {
          if(current_level < max_levels)
          {
            current_game_state = NEXT_LEVEL;
          }else if(current_level == max_levels)
          {
            current_game_state = COMPLETE;
          }
          score_reset();
        }				
        sfRenderWindow_clear(main_window,clear_color);	
        /*-----------------------------------------------------------------------------
         *  Set the current view
         *-----------------------------------------------------------------------------*/
        if(player != NULL){
          sfView_setCenter(main_view,sfSprite_getPosition(player->sprite));
          sfRenderWindow_setView(main_window,main_view);
        }
        /*-----------------------------------------------------------------------------
         *  Draw starfield
         *-----------------------------------------------------------------------------*/
        if(player != NULL){
          starfield_draw(main_view,main_window,sfSprite_getPosition(player->sprite));	
        }
        /*-----------------------------------------------------------------------------
         *  Draw objects
         *-----------------------------------------------------------------------------*/
        jnx_list *draw_queue = cartographer_get_at(main_view);
        if(draw_queue != NULL){
          jnx_node *current_draw_pos = draw_queue->head; 
          /*-----------------------------------------------------------------------------
           *  Draw weapon fire
           *-----------------------------------------------------------------------------*/
          weapon_draw(main_window,main_view,&draw_queue);
          while(current_draw_pos)
          {
            game_object *obj = (game_object*)current_draw_pos->_data;
            if(strcmp(obj->object_type,"player") == 0)
            {
              game_object_update(obj,current_event,main_view);				
            }
            else
            {
              /*-----------------------------------------------------------------------------
               *  Update AI objects
               *-----------------------------------------------------------------------------*/
              game_ai_update(obj,player);	
            }
#ifdef BOUNDING_BOX
            sfRectangleShape *bounding = game_object_get_boundingRect(obj);
            sfRenderWindow_drawRectangleShape(main_window,bounding,NULL);
            sfRectangleShape_destroy(bounding);
#endif
            sfRenderWindow_drawSprite(main_window,obj->sprite,NULL);
            current_draw_pos = current_draw_pos->next_node;
          }
          cartographer_update();
        }
        /*-----------------------------------------------------------------------------
         *  Draw ingame ui
         *-----------------------------------------------------------------------------*/
        game_ui_update(main_window,main_view,player,draw_queue);
        game_ui_draw(main_window,draw_queue);
        sfRenderWindow_display(main_window);
        jnx_list_destroy(&draw_queue);
        break;
      case GAMEOVER:
        sfRenderWindow_clear(main_window,clear_color);	
        sfRenderWindow_pollEvent(main_window,&current_event);
        switch(current_event.key.code)
        {
          case sfKeyEscape:
            sfRenderWindow_close(main_window);
            break;
        }
        sfVector2f pos = sfView_getCenter(main_view);
        int text_offset = strlen(sfText_getString(game_over_text));
        text_offset = text_offset * sfText_getCharacterSize(game_over_text);
        sfVector2f newpos;
        newpos.x	= pos.x - (text_offset / 2);
        newpos.y = pos.y;
        sfText_setPosition(game_over_text,newpos);
        sfRenderWindow_drawText(main_window,game_over_text,NULL);
        sfRenderWindow_display(main_window);	
        break;
      case GAMESTART:
        sfRenderWindow_clear(main_window,clear_color);	
        sfRenderWindow_pollEvent(main_window,&current_event);
        int game_bound = atoi(jnx_hash_get(config,"GAMEBOUNDS"));
        switch(current_event.key.code)
        {
          case sfKeyEscape:
            sfRenderWindow_close(main_window);
            break;
          case sfKeySpace:

            play_music(INGAMEMUSIC);
            current_game_state = LOADING;
            break;
        }
        sfVector2f pos_start = sfView_getCenter(main_view);
        int text_offset_start = strlen(sfText_getString(game_start_text));
        text_offset_start = text_offset_start * sfText_getCharacterSize(game_start_text);
        sfVector2f newpos_start;
        newpos_start.x	= pos_start.x + (text_offset_start /2 );
        newpos_start.y = pos_start.y - (text_offset_start /2);
        sfText_setPosition(game_start_text,newpos_start);

        sfVector2f button_start;
        button_start.x = newpos_start.x;
        button_start.y = newpos_start.y + 50;
        sfText_setPosition(game_start_button_text,button_start);
        button_start.y = button_start.y + 50;
        sfText_setPosition(game_author_text,button_start);
        switch(text_yellow)
        {
          case 0:
            sfText_setColor(game_start_button_text,sfColor_fromRGB(255,255,0));
            sfText_setColor(game_start_text,sfColor_fromRGB(255,255,0));
            text_yellow = 1;
            break;
          case 1:
            sfText_setColor(game_start_button_text,sfColor_fromRGB(255,0,0));
            sfText_setColor(game_start_text,sfColor_fromRGB(255,0,0));
            text_yellow = 0;
            break;
        }
        jnx_list *menu_starfield = starfield_menu_create(main_view);
        jnx_node *head = menu_starfield->head;
        while(menu_starfield->head)
        {
          sfSprite *sprite = menu_starfield->head->_data;
          sfVector2f pos = sfSprite_getPosition(sprite);
          pos.y = pos.y +5;
          if(pos.y > sfView_getSize(main_view).y)
          {
            pos.y = 0;
          }
          sfSprite_setPosition(sprite,pos);
          sfRenderWindow_drawSprite(main_window,sprite,NULL);
          menu_starfield->head = menu_starfield->head->next_node;
        }
        menu_starfield->head = head;
        sfRenderWindow_drawText(main_window,game_start_button_text,NULL);
        sfRenderWindow_drawText(main_window,game_start_text,NULL);
        sfRenderWindow_drawText(main_window,game_author_text,NULL);
        sfRenderWindow_display(main_window);	
        break;
      case LOADING:
        sfRenderWindow_clear(main_window,clear_color);
        sfRenderWindow_setView(main_window,main_view);
        if(loading_started == 0)
        {
          //spawn thread
          pthread_t load_worker;
          pthread_create(&load_worker,NULL,game_load,NULL);
          loading_started = 1;
        }
        if(loading_flag == 1)
        {
          //break and start game
          loading_started = 0;
          loading_flag = 0;
          sfVector2f viewpos;
          viewpos.x = game_bound /2;
          viewpos.y = game_bound /2;
          sfView_setCenter(main_view,viewpos);
          sfVector2f size = sfView_getSize(main_view);
          size.x = size.x *2;
          size.y = size.y *2;
          /*-----------------------------------------------------------------------------
           *  Set the zoom a bit further out so on level start we can zoom in
           *-----------------------------------------------------------------------------*/
          //	sfView_setSize(main_view,size);
          current_game_state = RUNNING;
          printf("Loading done\n");	
          break;
        }
        sfVector2f loadingpos = sfView_getCenter(main_view); 
        int loading_offset = strlen(sfText_getString(game_loading_text));
        loading_offset = loading_offset * sfText_getCharacterSize(game_loading_text);
        loadingpos.x = loadingpos.x - (loading_offset /2);
        loadingpos.y = loadingpos.y - (loading_offset /2);
        switch(text_yellow)
        {
          case 0:
            sfText_setColor(game_loading_text,sfColor_fromRGB(255,255,0));
            text_yellow = 1;
            break;
          case 1:
            sfText_setColor(game_loading_text,sfColor_fromRGB(255,0,0));
            text_yellow = 0;
            break;
        }
        sfText_setPosition(game_loading_text,loadingpos);
        sfRenderWindow_drawText(main_window,game_loading_text,NULL);
        sfRenderWindow_display(main_window);
        break;
    }
  }
}