Exemple #1
0
void Render () {
	policeDest.x = frameX.p - (POLICE_WIDTH / 2);
	policeDest.y = frameY.p - (POLICE_HEIGHT / 2);

	float angle = atan2(-frameY.v, -frameX.v) * (180.0 / M_PI);

	// Clear frame to black
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_RenderClear(renderer);

	// Draw police car
	SDL_RenderCopyEx(renderer, police, NULL, &policeDest, angle, NULL, SDL_FLIP_NONE);

	// Draw vectors
	SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
	SDL_RenderDrawLine(renderer, frameX.p, frameY.p, frameX.p+(frameX.a/5), frameY.p+(frameY.a/5));
	SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
	SDL_RenderDrawLine(renderer, frameX.p, frameY.p, frameX.p+(frameX.v/5), frameY.p+(frameY.v/5));

	// Flip frame
	SDL_RenderPresent(renderer);
}
/* Update (redraw) the screen*/
inline void UpdateScreen(double steer_angle, unsigned char direction, unsigned char pwm, unsigned char speed) {

	SDL_RenderClear(sdlRenderer); // trick: we don't need clear the render while updated textures are placed on updated background.

	/* Draw meters area */
	SDL_RenderCopy(sdlRenderer, sdlSpeedometerTexture, NULL, &sdlSpeedometerDstrect);
	SDL_RenderCopy(sdlRenderer, sdlPwmTexture, NULL, &sdlPwmDstrect);

	SDL_RenderCopyEx(sdlRenderer, sdlMeterArrowTexture, NULL, &sdlMeterArrowDstrect, ( (double) pwm ) - 135.0, &sdlMeterArrowCenterPoint, 0);
	SDL_RenderCopyEx(sdlRenderer, sdlPwmArrowTexture, NULL, &sdlPwmArrowDstrect, ( (double) speed ) - 135.0, &sdlPwmArrowCenterPoint, 0);

	/* Draw car scheme */
	SDL_RenderCopy(sdlRenderer, sdlCarTexture, NULL, &sdlCarDstrect);

	SDL_RenderCopyEx(sdlRenderer, sdlWheelTexture, NULL, &sdlLeftWheelDstrect, steer_angle, NULL, 0);
	SDL_RenderCopyEx(sdlRenderer, sdlWheelTexture, NULL, &sdlRightWheelDstrect, steer_angle, NULL, SDL_FLIP_HORIZONTAL);

	switch (direction) {
	case FORWARD:
		SDL_RenderCopyEx(sdlRenderer, sdlArrowTexture, NULL, &sdlRightArrowDstrect, 0, NULL, 0);
		SDL_RenderCopyEx(sdlRenderer, sdlArrowTexture, NULL, &sdlLeftArrowDstrect, 0, NULL, 0);
		//WriteText(5, 250, "FORWARD!", 26, sdlBlack); // debug
		break;
	case BACKWARD:
		SDL_RenderCopyEx(sdlRenderer, sdlArrowTexture, NULL, &sdlRightArrowDstrect, 0, NULL, SDL_FLIP_VERTICAL);
		SDL_RenderCopyEx(sdlRenderer, sdlArrowTexture, NULL, &sdlLeftArrowDstrect, 0, NULL, SDL_FLIP_VERTICAL);
		//WriteText(5, 250, "BACKWARD!", 26, sdlBlack); // debug
		break;
	case NONE:
		break;
	}

	SDL_RenderCopy(sdlRenderer, sdlTextTexture, NULL, &sdlTextDstrect);

	SDL_RenderPresent(sdlRenderer);
}
Exemple #3
0
// Blit to screen
bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed, double angle, int pivot_x, int pivot_y )
{
	bool ret = true;
	SDL_Rect rect;
	rect.x = (int) (camera.x * speed) + x * SCREEN_SIZE;
	rect.y = (int) (camera.y * speed) + y * SCREEN_SIZE;

	if(section != NULL)
	{
		rect.w = section->w;
		rect.h = section->h;
	}
	else
	{
		SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
	}

	rect.w *= SCREEN_SIZE;
	rect.h *= SCREEN_SIZE;

	SDL_Point* p = NULL;
	SDL_Point pivot;

	if(pivot_x != INT_MAX && pivot_y != INT_MAX)
	{
		pivot.x = pivot_x;
		pivot.y = pivot_y;
		p = &pivot;
	}

	if(SDL_RenderCopyEx(renderer, texture, section, &rect, angle, p, SDL_FLIP_NONE) != 0)
	{
		LOG("Cannot blit to screen. SDL_RenderCopy error: %s", SDL_GetError());
		ret = false;
	}

	return ret;
}
void GraphicsSystem::step(float /*delta_time*/)
{
	SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x5B, 0xFF);
	SDL_RenderClear(_renderer);

	for (unsigned int id : _entities) {
		Graphics *g = _entitymanager->get_component<Graphics>(id);
		if (!g->is_visible) {
			continue;
		}

		Transform *transform = _entitymanager->get_component<Transform>(id);

		/* create destination rect using the desired position
		 * and the texture's dimensions */
		SDL_Rect dest;
		dest.x = (int)transform->position.x;
		dest.y = (int)transform->position.y;
		dest.w = (int)(g->texture->getWidth() * transform->scale.x);
		dest.h = (int)(g->texture->getHeight() * transform->scale.y);

		const float radians_to_degrees = 57.324841f;
		const float rotation_degrees = transform->rotation.GetAngle() * radians_to_degrees;
		SDL_Point zero{ 0, 0 };
		bool success = SDL_RenderCopyEx(_renderer, g->texture->getTexture(), nullptr,
			&dest, rotation_degrees, &zero, SDL_FLIP_NONE) == 0;

		assert2(success, "Failed to render texture '%s'. SDL error: %s",
			g->texture->getName().c_str(), SDL_GetError());
	}

	for (auto& post_render_callback : _post_render_callbacks)
	{
		post_render_callback.function();
	}

	SDL_RenderPresent(_renderer);
}
Exemple #5
0
void Planet::Draw(b2Body *body) const
{
	if (Game::instance().SelectedLevel() == Game::Levels::INF)
	{
		Drawable::Draw(body);
	}

	static SDL_Point rot;
	rot.x = 0;
	rot.y = 2;
	if (Game::instance().SelectedWeapon() == Bomb::BombType::LASER)
	{
		laser_->Draw();
	}
	else if (weaponAim_.x != 0 || weaponAim_.y != 0)
	{
		b2Vec2 p1 = Screen::instance().toPixels(GetBody()->GetPosition(), true);
		unsigned int px = Screen::instance().pixelsPerMeter();

		SDL_Rect dst;
		dst.x = p1.x;
		dst.y = p1.y;
		dst.w = weaponAim_.Length() * px;
		dst.h = 5;

		float32 angle = (180 * atan2(weaponAim_.y, weaponAim_.x) / M_PI);
#ifdef __ANDROID__
		// There might be a bug in SDL...
		angle = -angle;
#endif
		SDL_RenderCopyEx(Screen::instance().renderer(), texture_, NULL, &dst, angle, &rot, SDL_FLIP_NONE);
	}

	if (Game::instance().SelectedLevel() != Game::Levels::INF)
	{
		Drawable::Draw(body);
	}
}
Exemple #6
0
void TextureManager::drawFrame(std::string texture_key, std::string frame_key,
                               int x, int y,
                               SDL_Renderer* pRenderer) {
  std::vector<int> coords = m_textureMap[texture_key]->getFrame(frame_key);

  if (!coords.empty()) {
    SDL_Rect srcRect;
    SDL_Rect dstRect;

    srcRect.x = coords[0];
    srcRect.y = coords[1];
    srcRect.w = dstRect.w = coords[2];
    srcRect.h = dstRect.h = coords[3];
    dstRect.x = x;
    dstRect.y = y;

    SDL_RenderCopyEx(pRenderer,
                     m_textureMap[texture_key]->getTexture(),
                     &srcRect, &dstRect,
                     0, 0,
                     SDL_FLIP_NONE);
  }
}
void Animation::Return_end(int x, int y, int angle, bool more, SDL_Renderer* render)
{
    if(more==true)
    {
        m_currentTime=SDL_GetTicks();
        SDL_Rect Destination{x, y, m_img_width, m_img_height};
        SDL_RenderCopyEx(render,m_sprite,&m_vector_frames[m_vector_index],&Destination,angle,NULL,m_flip);

        if(m_currentTime - m_startTime >= m_frame_duration)
        {
            if(m_return==false)
            {
                m_vector_index++;
            }
            else
                if(m_return==true)
                {
                    m_vector_index--;
                }

            if(unsigned(m_vector_index) == m_vector_frames.size())
            {
                m_vector_index--;
                m_return=true;
            }
            else
                if(m_vector_index == 0)
                {
                    m_return=false;
                }
            m_startTime = m_currentTime;
        }
    }
    else
        m_vector_index = 0;

}
Exemple #8
0
void internalDrawTextureSectionExt(Game* game, Texture* texture, Pointf position,
	Pointi sourcePosition, Pointi sourceSize, Pointf scale, float angle,
	Pointi origin, bool horizontalFlip, bool verticalFlip, float alpha)
{
	Pointi offset;
	offset.x = (sourceSize.x * (scale.x - 1)) / 2;
	offset.y = (sourceSize.y * (scale.y - 1)) / 2;
	
	SDL_Rect source = 
	{
		sourcePosition.x, sourcePosition.y, 
		sourceSize.x, sourceSize.y
	};
		
	SDL_Rect destination = 
	{
		position.x - offset.x, position.y - offset.y, 
		sourceSize.x * scale.x, sourceSize.y * scale.y
	};
	
	SDL_RendererFlip flip = SDL_FLIP_NONE;
	if(horizontalFlip)
		flip = (SDL_RendererFlip)(SDL_FLIP_HORIZONTAL | flip);
	if(verticalFlip)
		flip = (SDL_RendererFlip)(SDL_FLIP_VERTICAL | flip);
	
	SDL_Point originSdl = getSdlPointi(origin);
	
	if(alpha < 1.0f)
		SDL_SetTextureAlphaMod(texture->data, (int)(alpha * 255));
		
	SDL_RenderCopyEx(game->renderer, texture->data, &source, &destination,
		angle, &originSdl, flip);
		
	if(alpha < 1.0f)
		SDL_SetTextureAlphaMod(texture->data, 255);
}
Exemple #9
0
void draw_map(SDL_Renderer *r, GameMap *m) {

    unsigned int x = 0, y = 0;
    unsigned int c_count = 0, l_count = 0;
    char *t = NULL;

    for(c_count = 0; c_count < m->sz; c_count++) {
        t = m->t_map[c_count];
        for(l_count = 0; l_count < 31; l_count++) {
            // printf("Letra %c[%d %d]", *t, l_count, c_count);
            // printf(".%c", *t, l_count, c_count);
            SDL_Rect srect, drect;
            srect.x = srect.y = 0;
            srect.w = srect.h = 32;
            drect.x = l_count * 32; drect.y = c_count * 32;
            drect.w = drect.h = 32;

            if(*t == 'o')
                SDL_RenderCopy(r, m->wall, &srect, &drect);
            else if(*t == 'I')
                SDL_RenderCopyEx(r, m->wall, &srect, &drect, 90, NULL, SDL_FLIP_NONE);
            else if(*t == '#')
                SDL_RenderCopy(r, m->cross, &srect, &drect);
            else if(*t == '.')
                SDL_RenderCopy(r, m->pils, &srect, &drect);
            else if(*t == 'G')
                SDL_RenderCopy(r, m->ghost, &srect, &drect);
            else if(*t == 'Y')
                SDL_RenderCopy(r, m->special, &srect, &drect);
            else if(*t == 'P')
                SDL_RenderCopy(r, m->pacman, &srect, &drect);

            t++;
        }
        // printf("\n");
    }
}
Exemple #10
0
void TextureManager::render(const std::string & id,
                            const int & src_x,
                            const int & src_y,
                            const int & src_w,
                            const int & src_h,
                            const int & dest_x,
                            const int & dest_y,
                            const int & dest_w,
                            const int & dest_h,
                            SDL_Renderer * renderer,
                            const SDL_RendererFlip & flip)
{
    SDL_Rect src, dest;
    src.x = src_x;
    src.y = src_y;
    src.w = src_w;
    src.h = src_h;
    dest.x = dest_x;
    dest.y = dest_y;
    dest.w = dest_w;
    dest.h = dest_h;

    SDL_RenderCopyEx(renderer, textures[id], &src, &dest, 0, 0, flip);    
}
Exemple #11
0
/**
 * Renders the game's graphics.
 */
void doRender(gamestate* game)
{
	if (game->statusState == STATUS_STATE_LIVES)
	{
		draw_status_lives(game);
	}
	else if (game->statusState == STATUS_STATE_GAME)
	{	
		SDL_SetRenderDrawColor(game->renderer, 102, 204, 255, 255);
	
		SDL_RenderClear(game->renderer);

		for (int i = 0; i < 100; i++)
		{
			SDL_Rect tileRect = {game->tiles[i].x + game->scrollX, game->tiles[i].y, game->tiles[i].w, game->tiles[i].h};
			SDL_RenderCopy(game->renderer, game->tile, NULL, &tileRect);
		}

		SDL_Rect spookRect = {game->avatar.x + game->scrollX, game->avatar.y, 36, 36};
		SDL_RenderCopyEx(game->renderer, game->spook[game->avatar.aniFrame], NULL, &spookRect, 0, NULL, game->facingLeft);
	}

	SDL_RenderPresent(game->renderer);
}
Exemple #12
0
bool Game::Draw(SDL_Texture* Temp,
                int x, int y, int w, int h,
                SDL_RendererFlip Flip)
{
    //Rectangle for Position
    SDL_Rect dstrect;

    //Texture is empty
    if(Temp == NULL)
    {
        return false;
    }

    dstrect.x = x;
    dstrect.y = y;
    dstrect.w = w;
    dstrect.h = h;


    //Draw on the screen
	SDL_RenderCopyEx(Renderer, Temp, NULL, &dstrect, 0, NULL, Flip);

	return true;
}
Exemple #13
0
static void render(void) {
	SDL_Rect target = target_rect(&sky, -64, (REFERENCE_HEIGHT - BUILDING_HEIGHT / 4) - 128, 0.25f);
	target.y += sunset;
	SDL_RenderCopy(display.renderer, sky.texture, sky.src_rect, &target);
	for (size_t i = 0; i < MAX_THINGS; ++i) {
		thing_t *thing = &game.things[i];
		if (! thing->type) continue;
		sprite_t *sprite = sprite_thing_get(thing->type);
		target = target_rect(sprite, thing->position.x, thing->position.y, 1.f);
		
		if (thing->current_frame > sprite->frame_count) {
			LOG_WARN("%llu frame out of range!", (long long unsigned)i);
			thing->current_frame = 0;
		}
		
		SDL_RenderCopyEx(display.renderer, sprite->texture,
						 &sprite->src_rect[thing->current_frame], &target, thing->angle, NULL, 0);
	}
	
	if (camera.target.y > 0) {
		timer_blit(game.active_ticks, REFERENCE_WIDTH - 10, 10);
	}
	
}
Exemple #14
0
void Car::Draw(SDL_Renderer *renderer) {
    SDL_RenderCopyEx(renderer, this->texture, NULL, &this->rect, rotation, NULL, SDL_FLIP_NONE);
}
Exemple #15
0
void drawTexture(SDL_Rect rect  ,SDL_Texture *texture){
    int w,h;
    SDL_QueryTexture(texture, NULL, NULL, &w, &h);
    SDL_RenderCopyEx(renderer, texture, NULL, &rect, 0, NULL, SDL_FLIP_NONE);
}
Exemple #16
0
static void draw_tile(SDL_Renderer *r, tile const *t, SDL_Point const *pos, SDL_bool end, SDL_bool flip)
{
	SDL_Rect dest = (SDL_Rect) { pos->x - (end ? flip ? -t->box.w / 2 : 0 : t->box.x), pos->y - t->box.y, t->box.w / (end ? 2 : 1), t->box.h };
	SDL_RenderCopyEx(r, end ? t->end : t->main, 0, &dest, 0, 0, flip ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE);
}
Exemple #17
0
internal void
SDLDisplayBufferInWindow(sdl_offscreen_buffer *Buffer) {
    SDL_UpdateTexture(Buffer->Texture, 0, Buffer->Memory, Buffer->Pitch);
    SDL_RenderCopyEx(Buffer->Renderer, Buffer->Texture, 0, 0, 0, 0, SDL_FLIP_VERTICAL);
    SDL_RenderPresent(Buffer->Renderer);
}
Exemple #18
0
d_define_method_override(label, draw)(struct s_object *self, struct s_object *environment) {
    d_using(label);
    struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable);
    struct s_uiable_attributes *uiable_attributes = d_cast(self, uiable);
    struct s_environment_attributes *environment_attributes = d_cast(environment, environment);
    double position_x, position_y, dimension_w, dimension_h, center_x, center_y, width_factor, height_factor;
    int result = (intptr_t)d_call_owner(self, uiable, m_drawable_draw, environment); /* recall the father's draw method */
    SDL_Rect source, destination;
    SDL_Point center;
    if (label_attributes->image) {
        d_call(&(drawable_attributes->point_normalized_destination), m_point_get, &position_x, &position_y);
        d_call(&(drawable_attributes->point_normalized_dimension), m_point_get, &dimension_w, &dimension_h);
        d_call(&(drawable_attributes->point_normalized_center), m_point_get, &center_x, &center_y);
        width_factor = (dimension_w/label_attributes->last_width);
        height_factor = (dimension_h/label_attributes->last_height);
        source.x = 0;
        source.y = 0;
        destination.x = (position_x + uiable_attributes->border_w);
        destination.y = (position_y + uiable_attributes->border_h);
        if (label_attributes->format == e_label_background_format_fixed) {
            source.w = d_math_min((label_attributes->last_width - (uiable_attributes->border_w * 2.0)), label_attributes->string_width);
            source.h = d_math_min((label_attributes->last_height - (uiable_attributes->border_h * 2.0)), label_attributes->string_height);
            switch (label_attributes->alignment_x) {
                case e_label_alignment_center:
                    if ((source.x = d_math_max(((label_attributes->string_width-label_attributes->last_width)/2.0), 0)) == 0)
                        destination.x = position_x + (((label_attributes->last_width - label_attributes->string_width)/2.0) *
                                width_factor);
                    break;
                case e_label_alignment_right:
                    if ((source.x = d_math_max((label_attributes->string_width-label_attributes->last_width), 0)) == 0)
                        destination.x = position_x + ((label_attributes->last_width - label_attributes->string_width) * width_factor);
                default:
                    break;
            }
            switch (label_attributes->alignment_y) {
                case e_label_alignment_center:
                    if ((source.y = d_math_max(((label_attributes->string_height-label_attributes->last_height)/2.0), 0)) == 0)
                        destination.y = position_y + (((label_attributes->last_height - label_attributes->string_height)/2.0) *
                                height_factor);
                    break;
                case e_label_alignment_bottom:
                    if ((source.y = d_math_max((label_attributes->string_height-label_attributes->last_height), 0)) == 0)
                        destination.y = position_y + ((label_attributes->last_height - label_attributes->string_height) *
                                height_factor);
                default:
                    break;
            }
        } else if (label_attributes->format == e_label_background_format_adaptable) {
            source.w = label_attributes->string_width;
            source.h = label_attributes->string_height;
        }
        destination.w = source.w * width_factor;
        destination.h = source.h * height_factor;
        center.x = (position_x + center_x) - destination.x;
        center.y = (position_y + center_y) - destination.y;
        label_attributes->last_source = source;
        label_attributes->last_destination = destination;
        SDL_RenderCopyEx(environment_attributes->renderer, label_attributes->image, &source, &destination, drawable_attributes->angle, &center,
                (SDL_RendererFlip)drawable_attributes->flip);
    }
    d_cast_return(result);
}
 /**
  * Draw an SDL_Texture to an SDL_Renderer at some destination rect
  * taking a clip of the texture if desired
  * @param rend The renderer we want to draw too
  * @param tex The source texture we want to draw
  * @param dst The destination rectangle to render the texture too
  * @param clip The sub-section of the texture to draw (clipping rect)
  *       default of nullptr draws the entire texture
  */
 void render_texture(SDL_Renderer *ren, SDL_Texture *tex, SDL_Rect dst,
                     SDL_Rect *clip = nullptr, const double angle=0)
 {
     SDL_RenderCopyEx(ren, tex, clip, &dst, angle, nullptr, SDL_FLIP_NONE);
 }
Exemple #20
0
/*
 * meh_window_render_texture renders the given texture at the given position.
 */
void meh_window_render_texture(Window* window, SDL_Texture* texture, SDL_Rect* src, SDL_Rect* dst) {
	g_assert(window != NULL);
	g_assert(texture != NULL);

	SDL_RenderCopyEx(window->sdl_renderer, texture, src, dst, 0.0, NULL, SDL_FLIP_NONE);
}
static SDL_bool
WatchJoystick(SDL_Joystick * joystick)
{
    SDL_Window *window = NULL;
    SDL_Renderer *screen = NULL;
    SDL_Texture *background, *button, *axis, *marker;
    const char *name = NULL;
    SDL_bool retval = SDL_FALSE;
    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
    SDL_Event event;
    SDL_Rect dst;
    int s, _s;
    Uint8 alpha=200, alpha_step = -1;
    Uint32 alpha_ticks;
    char mapping[4096], temp[4096];
    MappingStep *step;
    MappingStep steps[] = {
        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
    };

    /* Create a window to display joystick axis position */
    window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                              SCREEN_HEIGHT, 0);
    if (window == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
        return SDL_FALSE;
    }

    screen = SDL_CreateRenderer(window, -1, 0);
    if (screen == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }
    
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
    SDL_RaiseWindow(window);

    /* scale for platforms that don't give you the window size you asked for. */
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);

    /* Print info about the joystick we are watching */
    name = SDL_JoystickName(joystick);
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
           name ? name : "Unknown Joystick");
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    
    SDL_Log("\n\n\
    ====================================================================================\n\
    Press the buttons on your controller when indicated\n\
    (Your controller may look different than the picture)\n\
    If you want to correct a mistake, press backspace or the back button on your device\n\
    To skip a button, press SPACE or click/touch the screen\n\
    To exit, press ESC\n\
    ====================================================================================\n");
    
    /* Initialize mapping with GUID and name */
    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());

    /* Loop, getting joystick events! */
    for(s=0; s<SDL_arraysize(steps) && !done;) {
        /* blank screen, set up for drawing this frame. */
        step = &steps[s];
        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
        step->axis = -1;
        step->button = -1;
        step->hat = -1;
        step->hat_value = -1;
        SDL_SetClipboardText("TESTING TESTING 123");
        
        switch(step->marker) {
            case MARKER_AXIS:
                marker = axis;
                break;
            case MARKER_BUTTON:
                marker = button;
                break;
            default:
                break;
        }
        
        dst.x = step->x;
        dst.y = step->y;
        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
        next=SDL_FALSE;

        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);

        while (!done && !next) {
            if (SDL_GetTicks() - alpha_ticks > 5) {
                alpha_ticks = SDL_GetTicks();
                alpha += alpha_step;
                if (alpha == 255) {
                    alpha_step = -1;
                }
                if (alpha < 128) {
                    alpha_step = 1;
                }
            }
            
            SDL_RenderClear(screen);
            SDL_RenderCopy(screen, background, NULL, NULL);
            SDL_SetTextureAlphaMod(marker, alpha);
            SDL_SetTextureColorMod(marker, 10, 255, 21);
            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
            SDL_RenderPresent(screen);
            
            if (SDL_PollEvent(&event)) {
                switch (event.type) {
                case SDL_JOYAXISMOTION:
                    if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
                        for (_s = 0; _s < s; _s++) {
                            if (steps[_s].axis == event.jaxis.axis) {
                                break;
                            }
                        }
                        if (_s == s) {
                            step->axis = event.jaxis.axis;
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
                            s++;
                            next=SDL_TRUE;
                        }
                    }
                    
                    break;
                case SDL_JOYHATMOTION:
                        if (event.jhat.value == SDL_HAT_CENTERED) {
                            break;  /* ignore centering, we're probably just coming back to the center from the previous item we set. */
                        }
                        for (_s = 0; _s < s; _s++) {
                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
                                break;
                            }
                        }
                        if (_s == s) {
                            step->hat = event.jhat.hat;
                            step->hat_value = event.jhat.value;
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
                            s++;
                            next=SDL_TRUE;
                        }
                    break;
                case SDL_JOYBALLMOTION:
                    break;
                case SDL_JOYBUTTONUP:
                    for (_s = 0; _s < s; _s++) {
                        if (steps[_s].button == event.jbutton.button) {
                            break;
                        }
                    }
                    if (_s == s) {
                        step->button = event.jbutton.button;
                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
                        s++;
                        next=SDL_TRUE;
                    }
                    break;
                case SDL_FINGERDOWN:
                case SDL_MOUSEBUTTONDOWN:
                    /* Skip this step */
                    s++;
                    next=SDL_TRUE;
                    break;
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
                        /* Undo! */
                        if (s > 0) {
                            SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping));
                            s--;
                            next = SDL_TRUE;
                        }
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_SPACE) {
                        /* Skip this step */
                        s++;
                        next=SDL_TRUE;
                        break;
                    }
                    
                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
                        break;
                    }
                    /* Fall through to signal quit */
                case SDL_QUIT:
                    done = SDL_TRUE;
                    break;
                default:
                    break;
                }
            }
        }

    }

    if (s == SDL_arraysize(steps) ) {
        SDL_Log("Mapping:\n\n%s\n\n", mapping);
        /* Print to stdout as well so the user can cat the output somewhere */
        printf("%s\n", mapping);
    }
    
    while(SDL_PollEvent(&event)) {};
    
    SDL_DestroyRenderer(screen);
    SDL_DestroyWindow(window);
    return retval;
}
void Enemy::Draw(Renderer &r)
{
	SDL_RenderCopyEx(r.getRender(), m_texture, NULL, m_render_rect, directionAngle, NULL,SDL_FLIP_NONE);
	
}
void sprite::render(SDL_Renderer *ren, bool isAnimated)
{
	//std::cout << "Render called" << std::endl;
	SDL_RenderCopyEx(ren, texture, &srcRect, &rect, NULL, NULL, flipSprite);
}
Exemple #24
0
static int graphicsrender_drawCropped_SDL(
        struct graphicstexture *gt, int x, int y, float alpha,
        unsigned int sourcex, unsigned int sourcey,
        unsigned int sourcewidth, unsigned int sourceheight,
        unsigned int drawwidth, unsigned int drawheight,
        int rotationcenterx, int rotationcentery, double rotationangle,
        int horiflipped,
        double red, double green, double blue, int textureFiltering) {
    // calculate source dimensions
    SDL_Rect src,dest;
    src.x = sourcex;
    src.y = sourcey;
    src.w = sourcewidth;
    src.h = sourceheight;

    // set target dimensinos
    dest.x = x;
    dest.y = y;
    dest.w = drawwidth;
    dest.h = drawheight;    

    // set alpha mod:
    int i = (int)((float)255.0f * alpha);
    if (SDL_SetTextureAlphaMod(gt->sdltex, i) < 0) {
        printwarning("Warning: Cannot set texture alpha "
        "mod %d: %s\n", i, SDL_GetError());
    }

    // calculate rotation center:
    SDL_Point p;
    p.x = (int)((double)rotationcenterx * ((double)drawwidth / src.w));
    p.y = (int)((double)rotationcentery * ((double)drawheight / src.h));

    // set draw color:
    SDL_SetTextureColorMod(gt->sdltex, (red * 255.0f),
        (green * 255.0f), (blue * 255.0f));

    // set texture filter:
    if (!textureFiltering) {
        // disable texture filter
        /* SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY,
        "0", SDL_HINT_OVERRIDE); */

        // this doesn't work. we will need to wait for:
        // https://bugzilla.libsdl.org/show_bug.cgi?id=2167
    }

    // actual draw call:
    if (horiflipped) {
        // draw rotated and flipped
        SDL_RenderCopyEx(mainrenderer, gt->sdltex, &src, &dest,
            rotationangle, &p, SDL_FLIP_HORIZONTAL);
    } else {
        if (rotationangle > 0.001 || rotationangle < -0.001) {
            // draw rotated
            SDL_RenderCopyEx(mainrenderer, gt->sdltex, &src, &dest,
            rotationangle, &p, SDL_FLIP_NONE);
        } else {
            // don't rotate the rendered image if it's barely rotated
            // (this helps the software renderer)
            SDL_RenderCopy(mainrenderer, gt->sdltex, &src, &dest);
        }
    }
    if (!textureFiltering) {
        // re-enable texture filter
        SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY,
        "1", SDL_HINT_OVERRIDE);
    }
    return 1;
}
Exemple #25
0
void MainLoop( )
{
    while( LGame.bRunning )
    { 
        TimingBegin( );
        
        // Gravity 
        LGame.GravityTime += DeltaTime;
        if( LGame.GravityTime >= LGame.ADD_GRAVITY_TIME )
        {
            LGame.Gravity += 0.35f;
            if( LGame.Gravity > LGame.MAX_GRAVITY )
            {
                LGame.Gravity = LGame.MAX_GRAVITY;
            }
            LGame.GravityTime = 0.f;

            LPlayer.Y += LGame.Gravity;
            if( LPlayer.Y > LPlayer.BaseGround )
            {
                LPlayer.Y = LPlayer.BaseGround;
            }            
        }
        else
        {
            LPlayer.Y = LPlayer.BaseGround;
        }
        
        // Render
        SDL_RenderClear( LGame.Renderer );
        for( size_t i = 0; i < LGame.Sprites.size( ); i++ )
        {
            SDL_Rect renderSrcQuad = { 0,0,0,0 };
            SDL_Rect renderQuad = { 0,0,0,0 };
            
            // Game
            if( LGame.Sprites[ i ]->Texture->Type == TT_GROUND )
            {
                LGame.Sprites[ i ]->SpriteLoc.X -= 2.25f;
                if( LGame.Sprites[ i ]->SpriteLoc.X + LGame.ScreenWidth <= 0 )
                {
                    LGame.Sprites[ i ]->SpriteLoc.X = float( LGame.ScreenWidth - 2 );
                }
                
                renderQuad.x = LGame.Sprites[ i ]->SpriteLoc.X;
                renderQuad.y = LGame.Sprites[ i ]->SpriteLoc.Y;
                renderQuad.w = LGame.Sprites[ i ]->Width;
                renderQuad.h = LGame.Sprites[ i ]->Height;
                
                 SDL_RenderCopy( LGame.Renderer, LGame.Sprites[ i ]->Texture->Image,  NULL, &renderQuad );
            }
            else if(  LGame.Sprites[ i ]->Texture->Type == TT_BACKGROUND )
            {                
                renderQuad.x = LGame.Sprites[ i ]->SpriteLoc.X;
                renderQuad.y = LGame.Sprites[ i ]->SpriteLoc.Y;
                renderQuad.w = LGame.Sprites[ i ]->Width;
                renderQuad.h = LGame.Sprites[ i ]->Height;
                
                 SDL_RenderCopy( LGame.Renderer, LGame.Sprites[ i ]->Texture->Image,  NULL, &renderQuad );
            }
            else if( LGame.Sprites[ i ]->Texture->Type == TT_PLAYER )
            {
                static int playerFrameCount;
                playerFrameCount++;
              
                if( playerFrameCount > 8 )
                {
                    playerFrameCount = 0;
                    LPlayer.CurrentXFrame += 32;
                    if( LPlayer.CurrentXFrame >= LPlayer.MaxX )
                    {
                        LPlayer.CurrentXFrame = 0;
                        LPlayer.CurrentYFrame += 32;
                        if( LPlayer.CurrentYFrame >= LPlayer.MaxY )
                        {
                            LPlayer.CurrentYFrame = 0;
                        }
                    }
                }
                
                renderSrcQuad.x = LPlayer.CurrentXFrame;
                renderSrcQuad.y = LPlayer.CurrentYFrame;
                renderSrcQuad.w = 32;
                renderSrcQuad.h = 32;
                
                renderQuad.x = LPlayer.X;
                renderQuad.y = LPlayer.Y;
                renderQuad.w = LGame.Sprites[ i ]->Width;
                renderQuad.h = LGame.Sprites[ i ]->Height;
                
                 SDL_RenderCopyEx( LGame.Renderer, LGame.Sprites[ i ]->Texture->Image, &renderSrcQuad, &renderQuad, 0, NULL, SDL_FLIP_HORIZONTAL );
            }
       }
        SDL_RenderPresent( LGame.Renderer );

        // Input
        SDL_Event e;
        while( SDL_PollEvent( &e ) )
        {
            if( e.type == SDL_QUIT )
            {
                LGame.bRunning = false;
            }
            else if( e.type == SDL_KEYUP )
            {
                switch( e.key.keysym.sym )
                {
                    case SDLK_ESCAPE:
                    LGame.bRunning = false;
                    break;
                    case SDLK_SPACE:
                    LGame.Gravity = -7.5f;
                    break;
                }
            }
        }
        
        TimingEnd( );
    }

    for( size_t i = 0; i < LGame.Sprites.size( ); i++ )
    {
        SDL_DestroyTexture( LGame.Sprites[ i ]->Texture->Image );
        free( LGame.Sprites[ i ] );
    }

    SDL_DestroyWindow( LGame.Window );
    SDL_DestroyRenderer( LGame.Renderer );
    SDL_FreeSurface( LGame.Surface );
    SDL_Quit( );
}
Exemple #26
0
void LTexture::renderStretched( SDL_Renderer* renderer, const int x, const int y, const SDL_Rect* stretchRect, const SDL_Rect* clip, const double angle, const SDL_Point* center, const SDL_RendererFlip flip ) const
{
    SDL_Rect renderQuad = { x, y, stretchRect->w, stretchRect->h };

    SDL_RenderCopyEx( renderer, mTexture, clip, &renderQuad, angle, center, flip );
}
Exemple #27
0
void GraphicsComponentRender( GraphicsComponent* component, SDL_Renderer* renderer )
{
    SDL_RenderCopyEx( renderer, component->texture, nullptr, component->rect,
                      component->rotation, nullptr, SDL_FLIP_NONE );
}
SDL_bool
WatchGameController(SDL_GameController * gamecontroller)
{
    /* This is indexed by SDL_GameControllerButton. */
    static const struct { int x; int y; } button_positions[] = {
        {387, 167},  /* A */
        {431, 132},  /* B */
        {342, 132},  /* X */
        {389, 101},  /* Y */
        {174, 132},  /* BACK */
        {233, 132},  /* GUIDE */
        {289, 132},  /* START */
        {75,  154},  /* LEFTSTICK */
        {305, 230},  /* RIGHTSTICK */
        {77,  40},   /* LEFTSHOULDER */
        {396, 36},   /* RIGHTSHOULDER */
        {154, 188},  /* DPAD_UP */
        {154, 249},  /* DPAD_DOWN */
        {116, 217},  /* DPAD_LEFT */
        {186, 217},  /* DPAD_RIGHT */
    };

    /* This is indexed by SDL_GameControllerAxis. */
    static const struct { int x; int y; double angle; } axis_positions[] = {
        {75,  154, 0.0},  /* LEFTX */
        {75,  154, 90.0},  /* LEFTY */
        {305, 230, 0.0},  /* RIGHTX */
        {305, 230, 90.0},  /* RIGHTY */
        {91, 0, 90.0},     /* TRIGGERLEFT */
        {375, 0, 90.0},    /* TRIGGERRIGHT */
    };

    const char *name = SDL_GameControllerName(gamecontroller);
    const char *basetitle = "Game Controller Test: ";
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    char *title = (char *)SDL_malloc(titlelen);
    SDL_Texture *background, *button, *axis;
    SDL_Window *window = NULL;
    SDL_Renderer *screen = NULL;
    SDL_bool retval = SDL_FALSE;
    SDL_bool done = SDL_FALSE;
    SDL_Event event;
    int i;

    if (title) {
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
    }

    /* Create a window to display controller state */
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
                              SCREEN_HEIGHT, 0);
    if (window == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
        return SDL_FALSE;
    }

    screen = SDL_CreateRenderer(window, -1, 0);
    if (screen == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }

    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(screen);
    SDL_RenderPresent(screen);
    SDL_RaiseWindow(window);

    /* scale for platforms that don't give you the window size you asked for. */
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);

    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);

    if (!background || !button || !axis) {
        SDL_DestroyRenderer(screen);
        SDL_DestroyWindow(window);
        return SDL_FALSE;
    }
    SDL_SetTextureColorMod(button, 10, 255, 21);
    SDL_SetTextureColorMod(axis, 10, 255, 21);

    /* !!! FIXME: */
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/

    /* Print info about the controller we are watching */
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");

    /* Loop, getting controller events! */
    while (!done) {
        /* blank screen, set up for drawing this frame. */
        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderCopy(screen, background, NULL, NULL);

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_KEYDOWN:
                if (event.key.keysym.sym != SDLK_ESCAPE) {
                    break;
                }
                /* Fall through to signal quit */
            case SDL_QUIT:
                done = SDL_TRUE;
                break;
            default:
                break;
            }
        }

        /* Update visual controller state */
        for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
            if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
                const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
                SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
            }
        }

        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
            const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
            const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
            if (value < -deadzone) {
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
                const double angle = axis_positions[i].angle;
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
            } else if (value > deadzone) {
                const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
                const double angle = axis_positions[i].angle + 180.0;
                SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
            }
        }

        SDL_RenderPresent(screen);

        if (!SDL_GameControllerGetAttached(gamecontroller)) {
            done = SDL_TRUE;
            retval = SDL_TRUE;  /* keep going, wait for reattach. */
        }
    }

    SDL_DestroyRenderer(screen);
    SDL_DestroyWindow(window);
    return retval;
}
Exemple #29
0
// enemy draw method
void Enemy::Draw(SDL_Renderer *renderer)
{
	SDL_RenderCopyEx(renderer, texture, NULL, &posRect, angle, &center, SDL_FLIP_NONE);
}
void loop(void *arg) {
  SDL_Event event;
  int i;
  SDL_GameController *gamecontroller = (SDL_GameController *)arg;

  /* blank screen, set up for drawing this frame. */
  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
  SDL_RenderClear(screen);
  SDL_RenderCopy(screen, background, NULL, NULL);

  while (SDL_PollEvent(&event)) {
    switch (event.type) {
    case SDL_KEYDOWN:
      if (event.key.keysym.sym != SDLK_ESCAPE) {
        break;
      }
    /* Fall through to signal quit */
    case SDL_QUIT:
      done = SDL_TRUE;
      break;
    default:
      break;
    }
  }

  /* Update visual controller state */
  for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
    if (SDL_GameControllerGetButton(
            gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
      const SDL_Rect dst = {button_positions[i].x, button_positions[i].y, 50,
                            50};
      SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
    }
  }

  for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
    const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
    const Sint16 value =
        SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
    if (value < -deadzone) {
      const SDL_Rect dst = {axis_positions[i].x, axis_positions[i].y, 50, 50};
      const double angle = axis_positions[i].angle;
      SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
    } else if (value > deadzone) {
      const SDL_Rect dst = {axis_positions[i].x, axis_positions[i].y, 50, 50};
      const double angle = axis_positions[i].angle + 180.0;
      SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
    }
  }

  SDL_RenderPresent(screen);

  if (!SDL_GameControllerGetAttached(gamecontroller)) {
    done = SDL_TRUE;
    retval = SDL_TRUE; /* keep going, wait for reattach. */
  }

#ifdef __EMSCRIPTEN__
  if (done) {
    emscripten_cancel_main_loop();
  }
#endif
}