Example #1
0
File: ui.c Project: evegard/tdt4258
void ui_state_title(void)
{
    int x = 0, y = 0, dx = 1, dy = 1, w = 20, h = 20, i = 0;

    snd_play("sounds/intro-valkyrie.raw");

    while (1) {
        image_draw(0, 0, img_bg);
        image_draw(x, y, img_soldier);
        screen_show_buffer();

        if (y + h == SCREEN_HEIGHT && dy == 1) dy = -1;
        if (y == 0 && dy == -1) dy = 1;
        if (x + w == SCREEN_WIDTH && dx == 1) dx = -1;
        if (x == 0 && dx == -1) dx = 1;
        x += dx; y += dy;

        led_setup = (i++ / 10) % 2 ? 0xaa : 0x55;
        led_update();

        if ((btn_ignore = btn_read()) > 0) {
            ui_state = UI_GAME_SOLDIER;
            game_init();
            return;
        }
    }
}
Example #2
0
File: ui.c Project: evegard/tdt4258
void ui_state_scoreboard(void)
{
    volatile int i;
    int x, y, dx, dy, w, h;
    image_t *bg, *sprite;

    ui_state = UI_TITLE;

    if (game_soldier_score == UI_MAX_POINTS) {
        bg = img_soldier_win;
        sprite = img_tank;
    } else {
        bg = img_tank_win;
        sprite = img_soldier;
    }

    w = sprite->width;
    h = sprite->height;

    snd_play_wait("sounds/point.raw");
    image_draw(0, 0, bg);

    x = rand() % 9 + 1; y = 0;
    dx = rand() % 3 + 1; dy = 1;

    while (1) {
        image_draw(x, y, sprite);
        screen_show_buffer();

        if (y + h == SCREEN_HEIGHT && dy >= 0) dy *= -0.8;
        if (x + w == SCREEN_WIDTH || x == 0) {
            x = rand() % 9 + 1; y = 0;
            dx = rand() % 3 + 1; dy = 1;
            if (rand() % 2) {
                x = SCREEN_WIDTH - w - x;
                dx *= -1;
            }
        }
        x += dx; x = MIN(x, SCREEN_WIDTH - w); x = MAX(x, 0);
        y += dy; y = MIN(y, SCREEN_HEIGHT - h);
        dy += 1;

        for (i = 0; i < 1000000; i++);

        if ((btn_ignore = btn_read()) > 0)
            return;
    }
}
Example #3
0
static struct image *
image_create(struct display *display, uint32_t key, const char *filename)
{
	struct image *image;
	gchar *basename;
	gchar *title;

	image = malloc(sizeof *image);
	if (image == NULL)
		return image;
	memset(image, 0, sizeof *image);

	basename = g_path_get_basename(filename);
	title = g_strdup_printf("Wayland Image - %s", basename);
	g_free(basename);

	image->filename = g_strdup(filename);

	image->window = window_create(display, title, 500, 400);
	image->display = display;

	/* FIXME: Window uses key 1 for moves, need some kind of
	 * allocation scheme here.  Or maybe just a real toolkit. */
	image->key = key + 100;

	window_set_user_data(image->window, image);
	window_set_redraw_handler(image->window, redraw_handler);
	window_set_keyboard_focus_handler(image->window,
					  keyboard_focus_handler);

	image_draw(image);

	return image;
}
Example #4
0
static void
redraw_handler(struct window *window, void *data)
{
	struct image *image = data;

	image_draw(image);
}
Example #5
0
File: main.c Project: odrevet/GE
game_status state_game_over(SDL_Surface *screen, game* p_game)
{
  bool done=false;
  image* game_over;
  game_over = image_load("./res/images/game_over.png");

  SDL_Rect srcrect = {0,0,160,144};
  SDL_Rect dstrect = set_rect(0,0,SCREEN_HEIGHT,SCREEN_WIDTH);

  while (!done)
    {
      //events
      SDL_Event event;
      while (SDL_PollEvent(&event))
        {
	  switch ( event.type )
            {
            case SDL_QUIT:
	      done = true;
	      break;
            case SDL_KEYUP:
	      switch ( event.key.keysym.sym )
                {
                default:break;
                }
            default:break;
            }
        }
      image_draw(game_over, get_backbuffer_surface(), 0, 0);
      SDL_SoftStretch(get_backbuffer_surface(), &srcrect, screen, &dstrect);
      SDL_Flip(screen);
    }

  return MAIN_MENU;
}
Example #6
0
static void DrawImageResourceAtPoint(gl_context_t* context, char const* image_name, GLfloat xFromLeft, GLfloat yFromTop) {
    image_t* img = bundle_image_named(image_name);
    rect2d_t r;
    r.size = image_size(img);
    r.origin.x = xFromLeft;
    r.origin.y = rect_top(context->screen_bounds) - r.size.height - yFromTop;
    image_draw(img, context, r);
    image_free(img);
}
Example #7
0
void map_editor_render(state_stack* stack) {
	state_desc *top = (state_desc*) table_ind(stack, stack->m_len-1);
	map_editor *mapEditor = (map_editor*) top->m_pData;

	map_render();

	rect tempSrc = _tile_rect(mapEditor->m_iActiveTile);
	rect tempDst = {1180, 32, 64, 64};
	image_draw(g_map.m_imageTiles, &tempSrc, &tempDst);
	font_print(g_font, 10, 10, "%s", g_map.m_cName);
	
	struct { int mode; const char *name; } modeNames[] = {
		{MAPEDITOR_NONE,	"How the f**k are you even here?"},
		{MAPEDITOR_EDIT,	"Edit Mode"},
		{MAPEDITOR_NAME,	"Rename Map"},
		{MAPEDITOR_SAVE,	"Saving Map"},
		{MAPEDITOR_LOAD,	"Loading Map"},
		{MAPEDITOR_TILE,	"Tile Select"},
		{MAPEDITOR_SHEET,	"Sheet Select"},
		{MAPEDITOR_WALK,	"Walk Edit"},
	};
	
	struct { int mode; const char *name; } walkModeNames[] = {
		{WALK_NONE, "NONE"},
		{WALK_WALK, "WALK"},
		{WALK_RUN, "RUN"},
		{WALK_SWIM, "SWIM"},
		{WALK_CLIMB, "CLIMB"},
		{WALK_FLY, "FLY"},
	};
	
	bool found = false;
	for (int i = 0; i < SIZE(modeNames); i++) {
		if (modeNames[i].mode == mapEditor->m_iMapEditorState) {
			font_print(g_font, 10, 690, modeNames[i].name);
			found = true;
		}
	}
	
	if (!found) {
		font_print(g_font, 10, 690, "How the f**k are you even here?");
		return;
	}
	
	if (mapEditor->m_iMapEditorState != MAPEDITOR_WALK) { return; }
	
	for (int i = 0; i < SIZE(walkModeNames); i++) {
		if (walkModeNames[i].mode == mapEditor->m_cMapWalk) {
			font_print(g_font, 1180, 32, walkModeNames[i].name);
			return;
		}
	}
	
	font_print(g_font, 1180, 32, "BROKEN");
	return;
}
Example #8
0
/**
 * Renders all of the surfaces onto the display surface.
 *
 * @param [in, out] rend The renderer for the window.
 * @param [in] game_data The current state of the game.
 */
void render(SDL_Renderer *rend, Game_Data *game_data) {
  for(uint16_t i = 0; i < game_data->battle_data.board.rows
        * game_data->battle_data.board.cols; i++) {
    if(game_data->battle_data.board.tiles[i].ent != NULL) {
      image_draw(&((Battle_Entity *)game_data->
                   battle_data.board.tiles[i].ent)->img, rend);
    }
  }
  
  SDL_RenderPresent(rend);
}
Example #9
0
File: main.c Project: odrevet/GE
game_status state_main_menu(SDL_Surface *screen, game* p_game)
{
  bool done=false;
  image* main_title;
  main_title = image_load("./res/images/main_title.png");
  SDL_Rect srcrect = {0,0,160,144};
  SDL_Rect dstrect = set_rect(0,0,SCREEN_HEIGHT,SCREEN_WIDTH);
  int next_state=STAY;
  while (!done)
    {
      //events
      SDL_Event event;
      while (SDL_PollEvent(&event))
        {
	  switch ( event.type )
            {
            case SDL_QUIT:
	      done = true;
	      break;
            default:
	      break;
            }
        }

      image_draw(main_title, get_backbuffer_surface(), 0, 0);
      SDL_SoftStretch(get_backbuffer_surface(), &srcrect, screen, &dstrect);
      SDL_Flip(screen);

      if(next_state == STAY)next_state = menu_main(screen, p_game);
      else if(next_state!=QUIT){
	switch(next_state){
	case LOAD:
	  menu_load(screen, &p_game);
	  next_state = state_in_game(screen, p_game);
	  break;
	case 1:
	  p_game = game_load("./res/scripts/state.xml");
	  next_state = state_in_game(screen, p_game);
	  break;
	default:
	  break;
	}
      }
      else if(next_state==QUIT)done=true;
    }

  image_free(main_title);

  return QUIT;
}
void drawcheckbox(HDC hdc,int x,int y,int wx,int wy,int checked,int active)
{
    RECT rect;
    int i=4+(active?1:0)+(checked?2:0);

    rect.left=x;
    rect.top=y;
    rect.right=x+wx;
    rect.bottom=y+wy;

    if(icon[i].bitmap)
        image_draw(hdc,&icon[i],x,y,x+wx,y+wy,0,HSTR|VSTR);
    else
        DrawFrameControl(hdc,&rect,DFC_BUTTON,DFCS_BUTTONCHECK|(checked?DFCS_CHECKED:0));
}
Example #11
0
void vgDrawImage(VGImage image)
{
   struct vg_context *ctx = vg_current_context();

   if (!ctx)
      return;

   if (image == VG_INVALID_HANDLE) {
      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
      return;
   }

   vg_validate_state(ctx);
   image_draw((struct vg_image*)image);
}
Example #12
0
void vegaDrawImage(VGImage image)
{
   struct vg_context *ctx = vg_current_context();

   if (!ctx)
      return;

   if (image == VG_INVALID_HANDLE) {
      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
      return;
   }

   vg_validate_state(ctx);
   image_draw(handle_to_image(image),
         &ctx->state.vg.image_user_to_surface_matrix);
}
Example #13
0
static int moonbase_image_draw( lua_State *s )
{
	void *image;
	struct rectangle r;

	image = *(void**)luaL_checkudata( s, 1, "moonbase_image" );
	luacom_read_array( s, 2, "ii", 1, &r.x, 2, &r.y );
	lua_len( s, 2 );
	if ( lua_tointeger(s, -1) == 4 ) {
		luacom_read_array( s, 2, "ii", 3, &r.w, 4, &r.h );
	} else {
		r.w = 0;
		r.h = 0;
	}
	image_draw( &r, image );
	return 0;
}
Example #14
0
int font_print(font Font, int iX, int iY, const char *cFmt, ...) {
	char cText[512];
	va_list vArgs;
	va_start(vArgs, cFmt);
	vsnprintf(cText, 512, cFmt, vArgs);
	va_end(vArgs);
	
	rect srcRect;
	rect dstRect = {iX, iY, Font.m_iGlyphWidth, Font.m_iGlyphHeight};
	
	for (char *ch = cText; *ch != '\0'; ch++) {
		srcRect = _glyph_rect(Font, *ch);
		image_draw(Font.m_image, &srcRect, &dstRect);
		dstRect.x += Font.m_iGlyphWidth;
	}
	return Font.m_iGlyphWidth * strlen(cText);
}
void box_draw(HDC hdc,int x1,int y1,int x2,int y2,int id)
{
    int i=box[id].index;

    if(id<0||id>=BOX_NUM)
    {
        log_err("ERROR in box_draw(): invalid id=%d\n",id);
        return;
    }
    if(i<0||i>=THEME_NM)
    {
        log_err("ERROR in box_draw(): invalid index=%d\n",i);
        return;
    }
    drawrect(hdc,x1,y1,x2,y2,D(i),D(i+1),D(i+2),D(i+3));
    if(box[id].big)
        image_draw(hdc,&box[id],x1,y1,x2,y2,D(i+5),D(i+6));
/*    else
        drawrect(hdc,x1,y1,x2,y2,0xFF,D(i+2),D(i+2),D(i+3));*/
}
Example #16
0
void vgWritePixels(const void * data, VGint dataStride,
                   VGImageFormat dataFormat,
                   VGint dx, VGint dy,
                   VGint width, VGint height)
{
   struct vg_context *ctx = vg_current_context();
   struct pipe_context *pipe = ctx->pipe;

   if (!supported_image_format(dataFormat)) {
      vg_set_error(ctx, VG_UNSUPPORTED_IMAGE_FORMAT_ERROR);
      return;
   }
   if (!data || !is_aligned(data)) {
      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
      return;
   }
   if (width <= 0 || height <= 0) {
      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
      return;
   }

   vg_validate_state(ctx);
   {
      struct vg_image *img = image_create(dataFormat, width, height);
      image_sub_data(img, data, dataStride, dataFormat, 0, 0,
                     width, height);
#if 0
      struct matrix *matrix = &ctx->state.vg.image_user_to_surface_matrix;
      matrix_translate(matrix, dx, dy);
      image_draw(img);
      matrix_translate(matrix, -dx, -dy);
#else
      /* this looks like a better approach */
      image_set_pixels(dx, dy, img, 0, 0, width, height);
#endif
      image_destroy(img);
   }
   /* make sure rendering has completed */
   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
}
Example #17
0
static void vg_render_glyph(struct vg_context *ctx,
                            struct vg_glyph *glyph,
                            VGbitfield paintModes,
                            VGboolean allowAutoHinting)
{
   if (glyph->object && paintModes) {
      struct vg_state *state = &ctx->state.vg;
      struct matrix m;

      m = state->glyph_user_to_surface_matrix;
      matrix_translate(&m,
            state->glyph_origin[0].f - glyph->glyph_origin[0],
            state->glyph_origin[1].f - glyph->glyph_origin[1]);

      if (glyph->object->type == VG_OBJECT_PATH) {
         path_render((struct path *) glyph->object, paintModes, &m);
      }
      else {
         assert(glyph->object->type == VG_OBJECT_IMAGE);
         image_draw((struct vg_image *) glyph->object, &m);
      }
   }
}
Example #18
0
File: main.c Project: odrevet/GE2
game_status state_in_game(SDL_Renderer *renderer, game* p_game)
{
  bool done=false;

  game_status ret_code = QUIT;
  map *p_map = p_game->p_map;

  while (!done){
    //draw
    SDL_SetRenderDrawColor(renderer, 0x42, 0x4C, 0x4C, 0xFF);
    SDL_RenderClear(renderer);

    //draw the map
    map_draw(p_game->p_map, renderer);
    
    //draw the chipset
    image_draw(p_map->p_chipset, renderer, 0, 0);

    //input
    SDL_Event event;
    while (SDL_PollEvent(&event)){

      if(event.type ==  SDL_QUIT){
        return QUIT;
      }

      const int cam_speed = 8;
      
      switch ( event.type )
	{
	case SDL_KEYDOWN:
	  switch (event.key.keysym.sym)
	    {
	    case SDLK_t:
	      //set tile mode
	      if(p_game->tile_mode == TILE)p_game->tile_mode = LINE;
	      else p_game->tile_mode = TILE;
	      break;
	    case SDLK_q:
	      //quit
	      done=true;
	      break;
	    case SDLK_l:
	      //set screen resolution
	      if(event.key.keysym.mod & KMOD_LSHIFT){
		SCREEN_WIDTH += cam_speed;
		SCREEN_HEIGHT += cam_speed;
	      }
	      else{
		SCREEN_WIDTH -= cam_speed;
		SCREEN_HEIGHT -= cam_speed;
	      }
	      
	      SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
	      break;	      
	    case SDLK_j:
	      //join mode (Z-Draw index = current depth layer)
	      p_game->join_mode = !p_game->join_mode;
	      if(p_game->join_mode)
		p_map->draw_enable_z = p_map->map_tile_z_index;
	      break;	      
	    case SDLK_z:
	      //set the Z-Draw index
	      if(event.key.keysym.mod & KMOD_LSHIFT){
		if(p_map->draw_enable_z < p_map->depth-1)
		  p_map->draw_enable_z++;
	      }
	      else if(p_map->draw_enable_z > 0)
		p_map->draw_enable_z--;

	      if(p_game->join_mode)
		p_map->map_tile_z_index = p_map->draw_enable_z;
	      break;
	      //save
	    case SDLK_s:
	      map_save(p_map, p_game->map_save_path);
	    break;
	    //set the depth layer to edit
	    case SDLK_d:
	      if(event.key.keysym.mod & KMOD_LSHIFT){
		if(p_map->map_tile_z_index < p_map->depth-1){
		  p_map->map_tile_z_index++;
		}
	      }
	      else if(p_map->map_tile_z_index > 0)
		p_map->map_tile_z_index--;

	      if(p_game->join_mode)
		p_map->draw_enable_z = p_map->map_tile_z_index;
	    break;
	    //resize the map
	    case SDLK_r:
	      state_resize_menu(renderer, p_game);
	      break;
	    //camera movement
	    case SDLK_p:
	      p_map->o_camera.z += cam_speed;
	      break;
	    case SDLK_m:
	       p_map->o_camera.z -= cam_speed;
	      break;	      
	      case SDLK_LEFT:
		p_map->o_camera.x -= cam_speed;
	      break;
	    case SDLK_RIGHT:
		p_map->o_camera.x += cam_speed;
	      break;
	    case SDLK_UP:
		p_map->o_camera.y -= cam_speed;
	      break;
	    case SDLK_DOWN:
		p_map->o_camera.y += cam_speed;
	      break;	      
	    default:
	      break;
	    }
	  break;
	case SDL_MOUSEBUTTONDOWN:
	  game_tile_click(p_game,
			  event.button.x,
			  event.button.y,
			  event.button.button);	  
	  break;
	case SDL_MOUSEMOTION:
	  map_mouse_move(p_map,
			 renderer,
			 event.button.x,
			 event.button.y);
	  break;	  
	case SDL_MOUSEWHEEL:
	  if(event.button.x < 0)
	    p_map->o_camera.y--;
	  else if(event.button.x > 0)
	    p_map->o_camera.y++;
	  break;
	default:
	  break;
	}
    }

    //update display
    SDL_RenderPresent(renderer);
  }
    
  return ret_code;
}
Example #19
0
int main(void) {
	int gd = DETECT, gm;
	srand((unsigned int)rand() % 65536);
	initgraph(&gd, &gm, NULL);
	
	a[0] = 0; a[1] = .20; a[2] = -.15; a[3] = .85;
	b[0] = 0; b[1] = -.26; b[2] = .28; b[3] = .04;
	c[0] = 0; c[1] = .23; c[2] = .26; c[3] = -.04;
	d[0] = .16; d[1] = .22; d[2] = .24; d[3] = .85;
	e[0] = 0; e[1] = 0; e[2] = 0; e[3] = 0;
	f[0] = 0; f[1] = 1.60; f[2] = .44; f[3] = 1.6;
	p[0] = 328; p[1] = 2621; p[2] = 4915; p[3] = 32767;
	xscale = 25;
	yscale = 25;
	xoffset = 300;
	yoffset = -50;
	cleardevice();
	setbkcolor(1);
	image_draw(10, 20000);
	wait();
	
	a[0] = 0; a[1] = .1; a[2] = .42; a[3] = .42;
	b[0] = 0; b[1] = 0; b[2] = -.42; b[3] = .42;
	c[0] = 0; c[1] = 0; c[2] = .42; c[3] = -.42;
	d[0] = .5; d[1] = .1; d[2] = .42; d[3] = .42;
	e[0] = 0; e[1] = 0; e[2] = 0; e[3] = 0;
	f[0] = 0; f[1] = .2; f[2] = .2; f[3] = .2;
	p[0] = 1638; p[1] = 6553; p[2] = 19660; p[3] = 32767;
	xscale = 750;
	yscale = 750;
	xoffset = 300;
	yoffset = 00;
	cleardevice();
	setbkcolor(1);
	image_draw(13, 20000);
	wait();
	
	a[0] = .5; a[1] = .5; a[2] = .5; a[3] = 0;
	b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0;
	c[0] = 0; c[1] = 0; c[2] = 0; c[3] = 0;
	d[0] = .5; d[1] = .5; d[2] = .5; d[3] = 0;
	e[0] = 0; e[1] = 1.; e[2] = .5; e[3] = 0;
	f[0] = 0; f[1] = 0; f[2] = .5; f[3] = 0;
	p[0] = 10813; p[1] = 21626; p[2] = 32767; p[3] = 32767;
	xscale = 180;
	yscale = 180;
	xoffset = 150;
	yoffset = -50;
	cleardevice();
	setbkcolor(7);
	image_draw(5, 20000);
	wait();
	
	cleardevice();
	setbkcolor(7);
	a[0] = .333; a[1] = .333; a[2] = .667; a[3] = 0;
	b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0;
	c[0] = 0; c[1] = 0; c[2] = 0; c[3] = 0;
	d[0] = .333; d[1] = .333; d[2] = .667; d[3] = 0;
	e[0] = 0; e[1] = 1.; e[2] = .5; e[3] = 0;
	f[0] = 0; f[1] = 0; f[2] = .5; f[3] = 0;
	p[0] = 10813; p[1] = 21626; p[2] = 32767; p[3] = 32767;
	xscale = 180;
	yscale = 180;
	xoffset = 150;
	yoffset = -50;
	image_draw(4, 50000);
	wait();
	
	cleardevice();
	setbkcolor(7);
	a[0] = .5; a[1] = .5; a[2] = .5; a[3] = .5;
	b[0] = 0; b[1] = 0; b[2] = 0; b[3] = 0;
	c[0] = 0; c[1] = 0; c[2] = 0; c[3] = 0;
	d[0] = .5; d[1] = .5; d[2] = .5; d[3] = .5;
	e[0] = 0; e[1] = .5; e[2] = 0; e[3] = .5;
	f[0] = 0; f[1] = 0; f[2] = .5; f[3] = .5;
	p[0] = 8192; p[1] = 16383; p[2] = 24575; p[3] = 32767;
	xscale = 250;
	yscale = 200;
	xoffset = 200;
	yoffset = -50;
	image_draw(1, 100000);
	wait();
}
Example #20
0
/* Because va_'s %c is not unsigned! */
int font_putc(font Font, int iX, int iY, unsigned char ucChar) {
	rect srcRect = _glyph_rect(Font, ucChar);
	rect dstRect = {iX, iY, Font.m_iGlyphWidth, Font.m_iGlyphHeight};
	image_draw(Font.m_image, &srcRect, &dstRect);
	return Font.m_iGlyphWidth;
}
Example #21
0
void SdfRenderer::render(QPainter *painter, const QRectF &bounds)
{
	current_size_x = static_cast<int>(bounds.width());
	current_size_y = static_cast<int>(bounds.height());
	mStartX = static_cast<int>(bounds.x());
	mStartY = static_cast<int>(bounds.y());
	this->painter = painter;
	QDomElement docElem = doc.documentElement();
	QDomNode node = docElem.firstChild();
	while(!node.isNull())
	{
		QDomElement elem = node.toElement();
		if(!elem.isNull())
		{
			if (elem.tagName()=="line")
			{
				line(elem);
			}
			else if(elem.tagName()=="ellipse")
			{
				ellipse(elem);
			}
			else if (elem.tagName() == "arc") {
				arc(elem);
			}
			else if(elem.tagName()=="background")
			{
				background(elem);
			}
			else if(elem.tagName()=="text")
			{
				draw_text(elem);
			}
			else if (elem.tagName()=="rectangle")
			{
				rectangle(elem);
			}
			else if (elem.tagName()=="polygon")
			{
				polygon(elem);
			}
			else if (elem.tagName()=="point")
			{
				point(elem);
			}
			else if(elem.tagName()=="path")
			{
				path_draw(elem);
			}
			else if(elem.tagName()=="stylus")
			{
				stylus_draw(elem);
			}
			else if(elem.tagName()=="curve")
			{
				curve_draw(elem);
			}
			else if(elem.tagName()=="image")
			{
				image_draw(elem);
			}
		}
		node = node.nextSibling();
	}
	this->painter = 0;
}
Example #22
0
static void DrawBeerLabel(gl_context_t* context, image_t* img) {
    rect2d_t r = rect_make(0, 0, 0, 0);
    r.size = image_size(img);
    image_draw(img, context, r);
}
Example #23
0
File: main.c Project: odrevet/GE2
game_status state_in_game(SDL_Renderer *renderer, game* p_game)
{
  bool done=false;

  game_status ret_code = QUIT;
  map *p_map = p_game->p_map;
  
  while (!done){
    //update display
    //clear
    SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF);
    SDL_RenderClear(renderer);

    //draw the map
    map_draw(p_map, renderer);

    //draw the chipset
    image_draw(p_map->p_chipset, renderer, 0, 0);
    
    //input
    SDL_Event event;
    while (SDL_PollEvent(&event)){

      if(event.type ==  SDL_QUIT){
	done=true;
	break;
      }

      const int cam_speed = 8;
    
    switch ( event.type )
      {
      case SDL_KEYDOWN:
	switch (event.key.keysym.sym)
	  {
	  case SDLK_q:
	    done=true;
	    break;
	  case SDLK_s:
	    map_save(p_map, p_game->map_save_path);
	    break;
	  case SDLK_LEFT:
	    p_map->o_camera.x -= cam_speed;
	    break;
	  case SDLK_RIGHT:
	    p_map->o_camera.x += cam_speed;
	    break;
	  case SDLK_UP:
	    p_map->o_camera.y -= cam_speed;
	    break;
	  case SDLK_DOWN:
	    p_map->o_camera.y += cam_speed;
	    break;
	  default:
	    break;
	  }
	break;
      case SDL_MOUSEBUTTONDOWN:
	map_tile_click(p_map,
		       event.button.x,
		       event.button.y,
		       event.button.button);	  
	break;
      case SDL_MOUSEMOTION:
	map_mouse_move(p_map,
		       renderer,
		       event.button.x,
		       event.button.y);
	break;	  
      case SDL_MOUSEWHEEL:
	if(event.button.x < 0)
	  p_map->o_camera.y--;
	else if(event.button.x > 0)
	  p_map->o_camera.y++;
	break;
      default:
	break;
      }
    }

    //render
    SDL_RenderPresent(renderer);
  }
    
  return ret_code;
}
Example #24
0
File: main.c Project: odrevet/GE
int state_options(SDL_Surface *screen)
{
    TTF_Font *font_menu = font_load("res/asia.ttf", 50);
    image *bg_image = image_load("res/options.jpg");
    Mix_Chunk *select = sample_load("res/select.wav");

    int i;
	//int initial_unit_nb = g_game.unit_nb;
    int font_quality = SOLID;
    int choice_current=1;
    SDL_Rect cur_pos;
    bool done = false;

    cur_pos.x = 10;
    cur_pos.y = 0;
    char sz_choice[20];

    #ifdef GEKKO
        unsigned int start=0;
        int timerset = false;
    #endif
SDL_EnableKeyRepeat(50, 10);
    while (!done)
    {
        #ifdef GEKKO
       unsigned int start=0;
       if (!timerset) {
            timerset=true;
            start=SDL_GetTicks();
        }

       unsigned int now=SDL_GetTicks();
           if ((now-start)>50) {
                timerset=false;
                SDL_JoystickUpdate();
                int joystate = SDL_JoystickGetHat(g_game.v_unit[0].joystick, 0);
                switch (joystate){
                    case SDL_HAT_DOWN:
                       if (choice_current <= 1){
                        sample_play(select);
                        choice_current++;
                        }
                    break;
                    case SDL_HAT_UP:
                        if (choice_current > 1){
                            sample_play(select);
                            choice_current--;
                        }
                    break;
                    case SDL_HAT_LEFT:
                        if (choice_current == 2)
                        {
                            if (g_game.block_fill >= 0.01)
                                g_game.block_fill -= 0.01;
                        }
                        else if (choice_current == 1)
                        {
                            if (g_game.unit_nb > 2)
                            {
                                g_game.unit_nb -= 1;
                            }
                        }
                    break;
                    case SDL_HAT_RIGHT:
                        if (choice_current == 2)
                        {
                            if (g_game.block_fill < 0.99)
                                g_game.block_fill += 0.01;
                        }
                        else if (choice_current == 1)
                        {
                            if (g_game.unit_nb < UNIT_MAX)
                            {
                                g_game.unit_nb++;
                            }
                        }
                    break;
                    default:
                    break;
                }
           }
        #endif


        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch ( event.type )
            {
            case SDL_QUIT:
                exit(EXIT_SUCCESS);
                break;
                case SDL_JOYBUTTONDOWN:
                    if(event.jbutton.button == 6){
                        done = true;
                    }
                break;
            case SDL_KEYDOWN:
                switch ( event.key.keysym.sym )
                {
                case SDLK_DOWN:
                    if (choice_current <= 1){
                        sample_play(select);
                        choice_current++;
                    }
                    break;
                case SDLK_UP:
                    if (choice_current > 1)
                    {
                        sample_play(select);
                        choice_current--;
                    }
                    break;
                case SDLK_LEFT:
                    if (choice_current == 2)
                    {
                        if (g_game.block_fill >= 0.01)
                            g_game.block_fill -= 0.01;
                    }
                    else if (choice_current == 1)
                    {
                        if (g_game.unit_nb > 2)
                        {
                            g_game.unit_nb--;
                        }
                    }
                    break;
                case SDLK_RIGHT:
                    if (choice_current == 2)
                    {
                        if (g_game.block_fill < 0.99)
                            g_game.block_fill += 0.01;
                    }
                    else if (choice_current == 1)
                    {
                        if (g_game.unit_nb < UNIT_MAX)
                        {
                            g_game.unit_nb++;
                        }
                    }
                    break;
                case SDLK_ESCAPE:
                    done = true;
                    break;
                default:
                    break;
                }
                break;
            }

            if(event.type == SDL_JOYBUTTONDOWN && event.jbutton.button == 6)done=true;
        }

        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 20, 20, 20));
        image_draw(bg_image, screen, 0, 0);


        for (i=1;i<=2;i++)
        {
            if (i == 1)
            {
                sprintf(sz_choice, "%s < %.d >", "Player", g_game.unit_nb);
            }
            else if ( i == 2)
            {

                sprintf(sz_choice, "%s < %.0f%% >", "Block fill", g_game.block_fill * 100);
            }

            if (choice_current == i)
            {
                font_quality = SHADED;
            }
            else font_quality = SOLID;

            cur_pos.y = i*100;
            SDL_Surface *font_surface;
            font_surface = font_create_surface(font_menu, 250, 120, 35, 10, 100, 100, 100, 0, sz_choice, font_quality);
            SDL_BlitSurface(font_surface, NULL, screen, &cur_pos);
            SDL_FreeSurface(font_surface);
        }
        SDL_Flip(screen);
    }
    TTF_CloseFont(font_menu);
    SDL_FreeSurface(bg_image->surface);
    SDL_EnableKeyRepeat(0, 0);
    return OPTION;
}
Example #25
0
File: ui.c Project: evegard/tdt4258
void ui_state_game(void)
{
    int i, x, y, go_x, go_y, result;
    int aimline_x1 = SCREEN_WIDTH - UI_TILE_WIDTH / 2,
        aimline_y1 = UI_Y_OFFSET + UI_TILE_HEIGHT / 2,
        aimline_x2, aimline_y2, strengthbar_width;
    int play_explosion = 0;
    int tank_direction, tank_strength, meter_direction;
    double radians;
    game_position_t result_pos;
    image_t *tile;

    led_clear();
    for (i = 0; i < game_soldier_score; i++)
        led_set(7 - i, 1);
    for (i = 0; i < game_tank_score; i++)
        led_set(i, 1);

    if (game_soldier_score >= UI_MAX_POINTS ||
        game_tank_score >= UI_MAX_POINTS) {
        ui_state = UI_SCOREBOARD;
        return;
    }

    game_reset();

    while (1) {
        screen_clear(color_black);
        for (x = 0; x < GAME_WIDTH; x++)
            for (y = 0; y < GAME_HEIGHT; y++) {
                tile = game_is_scorched[y][x] ? img_dirt : img_grass;
                image_draw(x * UI_TILE_WIDTH, y * UI_TILE_HEIGHT + UI_Y_OFFSET, tile);
            }

        go_x = game_player.x; go_y = game_player.y;
        switch (ui_player_direction) {
            case GAME_NORTH: go_y--; break;
            case GAME_SOUTH: go_y++; break;
            case GAME_WEST: go_x--; break;
            case GAME_EAST: go_x++; break;
        }

        switch (ui_state) {
            case UI_GAME_SOLDIER:
                if (go_x >= 0 && go_x < GAME_WIDTH &&
                    go_y >= 0 && go_y < GAME_HEIGHT) {
                    screen_rectangle(
                        go_x * UI_TILE_WIDTH,
                        go_y * UI_TILE_HEIGHT + UI_Y_OFFSET,
                        UI_TILE_WIDTH,
                        UI_TILE_HEIGHT,
                        color_blue);
                }

                if (ui_play_point_sound)
                    snd_play("sounds/point.raw");
                else if (play_explosion)
                    snd_play("sounds/eksplosjon.raw");
                ui_play_point_sound = 0;
                play_explosion = 0;

                if (btn_is_pushed(7)) {
                    result = game_move_player(ui_player_direction);
                    if (result == GAME_MOVE_TANK) {
                        ui_play_point_sound = 1;
                        return;
                    }
                    if (result == GAME_MOVE_OK) {
                        tank_direction = GAME_MIN_DIRECTION;
                        meter_direction = UI_DIRECTION_SPEED;
                        ui_state = UI_GAME_DIRECTION;
                    }
                }

                if (btn_is_pushed(6)) {
                    ui_player_direction = ((int)ui_player_direction + 1) % 4;
                }

                break;

            case UI_GAME_DIRECTION:
                if (btn_is_pushed(0)) {
                    tank_strength = GAME_MIN_STRENGTH;
                    meter_direction = UI_STRENGTH_SPEED;
                    ui_state = UI_GAME_STRENGTH;
                    break;
                }

                radians = tank_direction * M_PI / 180;
                aimline_x2 = aimline_x1 - (int)(cos(radians) * UI_TILE_WIDTH * 3);
                aimline_y2 = aimline_y1 + (int)(sin(radians) * UI_TILE_HEIGHT * 3);
                screen_line(aimline_x1, aimline_y1, aimline_x2, aimline_y2, color_red);

                if (tank_direction >= GAME_MAX_DIRECTION)
                    meter_direction = -UI_DIRECTION_SPEED;
                if (tank_direction <= GAME_MIN_DIRECTION)
                    meter_direction = UI_DIRECTION_SPEED;
                tank_direction += meter_direction;
                tank_direction = MAX(GAME_MIN_DIRECTION, tank_direction);
                tank_direction = MIN(GAME_MAX_DIRECTION, tank_direction);

                break;

            case UI_GAME_STRENGTH:
                if (btn_is_pushed(0)) {
                    snd_play_wait("sounds/skyte.raw");
                    result_pos = game_shoot_bullet(tank_direction, tank_strength);
                    ui_state = UI_GAME_SOLDIER;
                    play_explosion = !game_position_equals(result_pos,
                        GAME_SHOT_OOB);
                    if (result_pos.x < 0 && result_pos.y < 0) {
                        ui_play_point_sound = 1;
                        return;
                    }
                    break;
                }

                screen_line(aimline_x1, aimline_y1, aimline_x2, aimline_y2, color_red);
                strengthbar_width = tank_strength *
                    (SCREEN_WIDTH - UI_TILE_WIDTH) / GAME_MAX_STRENGTH;
                screen_fill_rectangle(
                    SCREEN_WIDTH - UI_TILE_WIDTH / 2 - strengthbar_width,
                    UI_TILE_HEIGHT / 2,
                    strengthbar_width,
                    UI_TILE_HEIGHT,
                    color_red);

                if (tank_strength >= GAME_MAX_STRENGTH)
                    meter_direction = -UI_STRENGTH_SPEED;
                if (tank_strength <= GAME_MIN_STRENGTH)
                    meter_direction = UI_STRENGTH_SPEED;
                tank_strength += meter_direction;
                tank_strength = MAX(GAME_MIN_STRENGTH, tank_strength);
                tank_strength = MIN(GAME_MAX_STRENGTH, tank_strength);

                break;
        }

        image_draw(
            (GAME_WIDTH - 2) * UI_TILE_WIDTH,
            UI_Y_OFFSET,
            img_tank);
        image_draw(
            game_player.x * UI_TILE_WIDTH,
            game_player.y * UI_TILE_HEIGHT + UI_Y_OFFSET,
            img_soldier);

        screen_show_buffer();
    }
}
Example #26
0
File: main.c Project: odrevet/GE
int state_main_menu(SDL_Surface *screen)
{
    TTF_Font *font_menu = font_load("res/asia.ttf", 50);
    TTF_Font *font_credit = font_load("res/asia.ttf", 20);
    image *bg_image = image_load("res/main.jpg");
    Mix_Chunk *select = sample_load("res/select.wav");

    int i;
    const int item_nb = 2;
    int font_quality = SOLID;
    int choice_current=1;
    SDL_Rect cur_pos;
    bool done = false;
	//bool is_in_game = false;

    cur_pos.x = 10;
    cur_pos.y = 0;
    char sz_choice[20];

    #ifdef GEKKO
    bool timerset = false;
    #endif

    while (!done)
    {
        #ifdef GEKKO
       unsigned int start=0;
       if (!timerset) {
            timerset=true;
            start=SDL_GetTicks();
        }

       unsigned int now=SDL_GetTicks();
       if ((now-start)>50) {
            timerset=false;
            SDL_JoystickUpdate();
            int joystate = SDL_JoystickGetHat(g_game.v_unit[0].joystick, 0);
            switch (joystate){
                case SDL_HAT_DOWN:
                    if (choice_current < item_nb){
                        choice_current++;
                        sample_play(select);
                    }
                break;
                case SDL_HAT_UP:
                    if (choice_current > 1){
                        choice_current--;
                        sample_play(select);
                    }
                break;
            }
       }
       #endif



        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch ( event.type )
            {
            case SDL_QUIT:
                done = true;
                break;
            case SDL_KEYUP:
                switch ( event.key.keysym.sym )
                {
                case SDLK_DOWN:
                    if (choice_current < item_nb){
                        choice_current++;
                        sample_play(select);
                    }
                    break;
                case SDLK_UP:
                    if (choice_current > 1){
                        choice_current--;
                        sample_play(select);
                    }
                    break;
                case SDLK_RETURN:
                    while (choice_current == IN_GAME)
                    {
                        unit_default_param(g_game.v_unit, g_game.unit_nb);
                        choice_current = state_in_game(screen);
                        if (choice_current == MAIN_MENU)    //The current state is main menu, select in game as default choice and quit
                        {
                            choice_current = IN_GAME;
                            break;
                        }
                    }
                    if (choice_current == OPTION)state_options(screen);
                    break;

                default:
                    break;
                }
            break;
            case SDL_JOYBUTTONDOWN:
                switch(event.jbutton.button)
                {
                    case 0:
                    break;
                    case 1:
                    break;

                    case 2:
                        while (choice_current == IN_GAME)
                        {
                            unit_default_param(g_game.v_unit, g_game.unit_nb);
                            choice_current = state_in_game(screen);
                            if (choice_current == MAIN_MENU)    //The current state is main menu, select in game as default choice and quit
                            {
                                choice_current = IN_GAME;
                                break;
                            }
                        }
                        if (choice_current == OPTION)state_options(screen);
                    break;

                    case 3:
                    break;

                    case 4:
                    break;

                    case 5:
                    break;

                    case 6:
                        done = true;
                    break;
                }
            break;
            default:
            break;
            }
        }

        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
        image_draw(bg_image, screen, 0, 0);

        int top_shift = 150;

        for (i=1;i<=2;i++)
        {
            if (i == 1)
            {
                strncpy(sz_choice, "Play", 20);
                sz_choice[19] = '\0';
            }
            else if ( i == 2)
            {
                strncpy(sz_choice, "Options", 20);
                sz_choice[19] = '\0';
            }

            if (choice_current == i)
            {
                font_quality = SHADED;
            }
            else font_quality = SOLID;

            cur_pos.y = top_shift + i*50;
            SDL_Surface *font_surface= font_create_surface(font_menu, 250, 120, 35, 10, 100, 100, 100, 0, sz_choice, font_quality);
            SDL_BlitSurface(font_surface, NULL, screen, &cur_pos);
            SDL_FreeSurface (font_surface);
        }

        cur_pos.y = 450;
        SDL_Surface *font_surface= font_create_surface(font_credit, 250, 120, 35, 10, 100, 100, 100, 0, "2010 - 2015 TheDrev", SOLID);
        SDL_BlitSurface(font_surface, NULL, screen, &cur_pos);
        SDL_FreeSurface (font_surface);

        SDL_Flip(screen);
    }

    //free font
    if(font_menu != NULL)TTF_CloseFont(font_menu);
    if(font_credit != NULL)TTF_CloseFont(font_credit);
    SDL_FreeSurface(bg_image->surface);
    Mix_FreeChunk(select);

    //free ressouces
    element* p_browse = images;
    while (p_browse) {
        ressource *res = p_browse->data;
        free(res->name);
        image_free(res->data);
        p_browse = p_browse->next;
    }

    linked_list_clear(&images);

    return EXIT_SUCCESS;
}