示例#1
0
文件: main.c 项目: tisyang/learnsdl2
int main(int argc, char *argv[])
{
    if(init() == false) {
        printf("Failed to initialize!\n");
        return 1;
    }
    if(load_media() == false) {
        printf("Failed to load media!\n");
        return 1;
    }
    bool quit = false;
    SDL_Event e;

    while(!quit) {
        while(SDL_PollEvent(&e) != 0) {
            if(e.type == SDL_QUIT) {
                quit = true;
            }
        }
        SDL_SetRenderDrawColor(g_renderer, 0xFF, 0xFF, 0xFF, 0xFF);
        SDL_RenderClear(g_renderer);

        render_texture(g_renderer, g_sprite, 0, 0, &g_spriteclips[0]);
        render_texture(g_renderer, g_sprite, SCREEN_WIDTH - g_spriteclips[1].w, 0, &g_spriteclips[1]);
        render_texture(g_renderer, g_sprite, 0, SCREEN_HEIGHT - g_spriteclips[2].h, &g_spriteclips[2]);
        render_texture(g_renderer, g_sprite, SCREEN_WIDTH - g_spriteclips[3].w, SCREEN_HEIGHT - g_spriteclips[3].h, &g_spriteclips[3]);

        SDL_RenderPresent(g_renderer);
    }
    close();
    return 0;
}
示例#2
0
static void
greets_display_effect (sync_info *sync, void *params, int iparam)
{
  greets_data *gdata = (greets_data *) params;
  f32 indmtx[2][3] = { { 0.5, 0.0, 0.0 },
		       { 0.0, 0.5, 0.0 } };
  float scroll = (float) sync->time_offset / 900.0;
  int i, startpos;

  /*world_set_pos_lookat_up (gdata->world,
			   (guVector) spooky_ghost_data_0.scene.pos,
			   (guVector) spooky_ghost_data_0.scene.lookat,
			   (guVector) spooky_ghost_data_0.scene.up);*/

  GX_SetIndTexMatrix (GX_ITM_0, indmtx, 5);

  world_display (gdata->world);
  GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
  
  shader_load (gdata->tile_shader);

  startpos = (int) (scroll * 8.0) - 32;

  for (i = 0; i < 4; i++)
    put_text (gdata->tileidx, i, 0, message, sizeof (message) - 1,
	      startpos * 4 + i, 32);

  GX_SetTevColor (GX_TEVREG0, (GXColor) { 255, 255, 255, 255 });
  render_texture (gdata, 5.5, scroll);

  //screenspace_rect (gdata->tile_shader, GX_VTXFMT0, 0);
}
示例#3
0
void SDL::render_texture (
    texture_ptr::pointer tex, renderer_ptr::pointer ren, int x, int y)
{
  int w, h;
  SDL_QueryTexture (tex, nullptr, nullptr, &w, &h);
  render_texture (tex, ren, x, y, w, h);
}
int SceneryManager::render_background(SDL_Renderer * renderer, float camera_x, float camera_y) {
    Scenery * scenery;
    for (std::vector<Scenery*>::iterator i = backgrounds.begin(); i != backgrounds.end(); i++) {
        scenery = *i;
             
        render_texture(scenery->texture, renderer, 
            scenery->x - (camera_x * scenery->scroll_rate), 
            scenery->y - (camera_y * scenery->scroll_rate));        
    }
    return 0; // success
}
示例#5
0
	void view3d_widget::handle_draw() const
	{
		// render to fbo
		render_fbo();

		// render texture
		render_texture();

		for(auto child : children_) {
			child->draw();
		}
	}
示例#6
0
    /**
      * Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
      * the texture's width and height and taking a clip of the texture if desired
      * If a clip is passed, the clip's width and height will be used instead of
      *    the texture's
      * @param rend The renderer we want to draw too
      * @param tex The source texture we want to draw
      * @param x The x coordinate to draw too
      * @param y The y coordinate to draw 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, int x, int y,
                        SDL_Rect *clip = nullptr, const double angle=0)
    {
        SDL_Rect dst;
        dst.x = x;
        dst.y = y;
        if (clip != nullptr){
            dst.w = clip->w;
            dst.h = clip->h;
        }
        else
            SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);

        render_texture(ren, tex, dst, clip);
    }
示例#7
0
int HSV::start(const std::vector<CL_String> &args)
{
	CL_DisplayWindow window("ClanLib HSV Example", 1024, 768);
	CL_Slot slot = window.sig_window_close().connect(this, &HSV::on_close);
	CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &HSV::on_input_up);

	CL_GraphicContext gc = window.get_gc();
	CL_InputContext ic = window.get_ic();
	CL_ProgramObject program = create_shader_program(gc);
	CL_Texture texture = create_texture(gc);

	CL_Font font(gc, "tahoma", 24);

	unsigned int last_time = CL_System::get_time();

	float hue_offset = 0.0;
	while (!quit)
	{
		unsigned int current_time = CL_System::get_time();
		float time_delta_ms = static_cast<float> (current_time - last_time);
		last_time = current_time;

		if (ic.get_keyboard().get_keycode(CL_KEY_LEFT))
			hue_offset += 0.0005f * time_delta_ms;
		else if (ic.get_keyboard().get_keycode(CL_KEY_RIGHT))
			hue_offset -= 0.0005f * time_delta_ms;
		if (hue_offset < -1.0f)
			hue_offset += 1.0f;
		if (hue_offset > 1.0f)
			hue_offset -= 1.0f;

		render_texture(gc, program, texture, hue_offset);

		font.draw_text(gc, 32, 700, "Use cursor keys left and right");
		window.flip();
		CL_KeepAlive::process(10);
	}

	return 0;
}
示例#8
0
文件: hsv.cpp 项目: Zenol/clanLib-3
int HSV::start(const std::vector<std::string> &args)
{
	DisplayWindow window("ClanLib HSV Example", 1024, 768);
	Slot slot = window.sig_window_close().connect(this, &HSV::on_close);
	Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &HSV::on_input_up);

	Canvas canvas(window);
	InputContext ic = window.get_ic();
	ProgramObject program = create_shader_program(canvas);
	Texture texture = create_texture(canvas);

	clan::Font font(canvas, "tahoma", 24);

	ubyte64 last_time = System::get_time();

	float hue_offset = 0.0;
	while (!quit)
	{
		ubyte64 current_time = System::get_time();
		float time_delta_ms = static_cast<float> (current_time - last_time);
		last_time = current_time;

		if (ic.get_keyboard().get_keycode(keycode_left))
			hue_offset += 0.0005f * time_delta_ms;
		else if (ic.get_keyboard().get_keycode(keycode_right))
			hue_offset -= 0.0005f * time_delta_ms;
		if (hue_offset < -1.0f)
			hue_offset += 1.0f;
		if (hue_offset > 1.0f)
			hue_offset -= 1.0f;

		render_texture(canvas, program, texture, hue_offset);

		font.draw_text(canvas, 32, 700, "Use cursor keys left and right");
		window.flip();
		KeepAlive::process(10);
	}

	return 0;
}
示例#9
0
文件: render.c 项目: dumbbell/piglit
static bool
buffer_test(const struct fmt_test *test)
{
	GLuint tex, tbo;

	/* Setup expected value, alpha is always max in the test. */
	unsigned short tbo_data[4] = { 0 };
	value_for_format(test, tbo_data);
	tbo_data[3] = get_max_value(test->type);

	glGenBuffers(1, &tbo);
	glBindBuffer(GL_TEXTURE_BUFFER, tbo);
	glBufferData(GL_TEXTURE_BUFFER, sizeof(tbo_data), tbo_data,
		     GL_STATIC_DRAW);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_BUFFER, tex);

	glTexBuffer(GL_TEXTURE_BUFFER, test->iformat, tbo);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return false;

	glUseProgram(buf_prog);
	glUniform1i(0 /* explicit loc */, 0);

	render_texture(tex, GL_TEXTURE_BUFFER, 0);

	if (!verify_contents_float(test))
		return false;

	piglit_present_results();

	glDeleteTextures(1, &tex);

	glBindBuffer(GL_TEXTURE_BUFFER, 0);

	return true;
}
示例#10
0
文件: render.c 项目: dumbbell/piglit
static bool
test_format(const struct fmt_test *test)
{
	bool pass = true;

	glUseProgram(prog);
	glUniform1i(0 /* explicit loc */, 0);

	/* Test glRenderbufferStorage. */
	GLuint rbo = create_and_bind_rbo(test);
	if (!rbo || !piglit_check_gl_error(GL_NO_ERROR)) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format 0x%x RBO test",
					     test->iformat);
		pass &= false;
	} else {
		piglit_report_subtest_result(PIGLIT_PASS,
					     "format 0x%x RBO test",
					     test->iformat);
	}
	glDeleteRenderbuffers(1, &rbo);

	/* Create framebuffer object. */
	GLuint fbo_tex;
	const GLuint fbo = create_and_bind_fbo(test, &fbo_tex);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
		GL_FRAMEBUFFER_COMPLETE) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format 0x%x fbo fail",
					     test->iformat);
		pass &= false;
	}

	/* Create a texture, upload data */
	const GLuint texture = create_and_bind_texture(test);

	render_texture(texture, GL_TEXTURE_2D, fbo);

	glDeleteTextures(1, &texture);

	/* Test glCopyTexImage2D by copying current fbo content to
	 * a texture, rendering copy back to fbo and verifying fbo contents.
	 */
	GLuint tmp_tex = create_and_bind_empty_texture();
	glCopyTexImage2D(GL_TEXTURE_2D, 0, test->iformat, 0, 0, piglit_width,
			 piglit_height, 0);

	render_texture(tmp_tex, GL_TEXTURE_2D, fbo);

	glDeleteTextures(1, &tmp_tex);

	/* Verify contents. */
	pass &= verify_contents(test);

	glDeleteFramebuffers(1, &fbo);

	/* Render fbo contents to window. */
	render_texture(fbo_tex, GL_TEXTURE_2D, 0);

	piglit_present_results();

	glDeleteTextures(1, &fbo_tex);

	return pass;
}
示例#11
0
int main(int argc, char **argv){
    if(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG != IMG_INIT_PNG){
        err_msg("IMG_INIT");
        return 1;
    }
    
    if (SDL_Init(SDL_INIT_EVERYTHING)){
        //std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        err_msg("SDL_Init");
        return 1;
    }
    SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT,
            SDL_WINDOW_SHOWN);
    if (!win){
        //std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        err_msg("CreateWindow");
        return 1;
    }
    SDL_Renderer *ren = SDL_CreateRenderer(win, -1, 
            SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (!ren){
        //std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        err_msg("CreateRenderer");
        return 1;
    }
    
    SDL_Texture *background = load_texture("background.png", ren);
    SDL_Texture *image = load_texture("image.png", ren);
    if(!((long)background & (long)image)){
        err_msg("Load Images");
        return 1;
    }
    SDL_RenderClear(ren);
    int w, h;
    SDL_QueryTexture(background, NULL, NULL, &w, &h);
    /*for(int i = 0; i < 4; i++){
        render_texture(background, ren, (i & 1) * w, ((i >> 1) & 1) * h);
    }*/
    int tile_x = SCREEN_WIDTH / TILE_SIZE;
    int tile_y = SCREEN_HEIGHT / TILE_SIZE;

    for(int i = 0; i < tile_x * tile_y; i++){
        int x = i % tile_x;
        int y = i / tile_x;
        render_texture(background, ren, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
    }
   
    int iW, iH;
    SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
    int x = SCREEN_WIDTH / 2 - iW / 2;
    int y = SCREEN_HEIGHT / 2 - iH / 2;
    render_texture(image, ren, x, y);

    SDL_RenderPresent(ren);
    SDL_Delay(2000);
    SDL_DestroyTexture(background);
    SDL_DestroyTexture(image);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();






}   
示例#12
0
文件: render.c 项目: dumbbell/piglit
static bool
test_format(const struct fmt_test *test)
{
	bool pass = true;

	if (piglit_is_extension_supported("GL_OES_texture_buffer") &&
	    test->can_texbuf) {
		bool buf_test = buffer_test(test);
		piglit_report_subtest_result(PIGLIT_RESULT(buf_test),
					     "format 0x%x TBO test",
					     test->iformat);
		pass &= buf_test;
	}

	glUseProgram(prog);
	glUniform1i(0 /* explicit loc */, 0);

	/* Create a texture, upload data */
	const GLuint texture = create_texture(test);

	glBindTexture(GL_TEXTURE_2D, texture);

	/* Can only texture from. */
	if (!test->req_render) {
		/* Render texture to window and verify contents. */
		render_texture(texture, GL_TEXTURE_2D, 0);
		bool render_test = verify_contents_float(test);
		piglit_present_results();
		piglit_report_subtest_result(PIGLIT_RESULT(render_test),
					     "format 0x%x",
					     test->iformat);
		glDeleteTextures(1, &texture);
		pass &= render_test;
		return pass;
	}

	/* Test glRenderbufferStorage. */
	GLuint rbo = create_rbo(test);
	if (!rbo || !piglit_check_gl_error(GL_NO_ERROR)) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format 0x%x RBO test",
					     test->iformat);
		pass &= false;
	} else {
		piglit_report_subtest_result(PIGLIT_PASS,
					     "format 0x%x RBO test",
					     test->iformat);
	}
	glDeleteRenderbuffers(1, &rbo);

	/* Create framebuffer object. */
	GLuint fbo_tex;
	const GLuint fbo = create_fbo(test, &fbo_tex);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
		GL_FRAMEBUFFER_COMPLETE) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format 0x%x fbo fail",
					     test->iformat);
		pass &= false;
	}

	render_texture(texture, GL_TEXTURE_2D, fbo);

	/* Test glCopyTexImage2D by copying current fbo content to
	 * a texture, rendering copy back to fbo and verifying fbo contents.
	 */
	GLuint tmp_tex = create_empty_texture();
	glCopyTexImage2D(GL_TEXTURE_2D, 0, test->iformat, 0, 0, piglit_width,
			 piglit_height, 0);

	render_texture(tmp_tex, GL_TEXTURE_2D, fbo);

	/* If format can be read, verify contents. */
	if (test->can_read)
		pass &= verify_contents(test);

	glDeleteTextures(1, &tmp_tex);

	/* If GL_EXT_copy_image is supported then create another
	 * texture, copy contents and render result to fbo.
	 */
	GLuint texture_copy = 0;
	if (piglit_is_extension_supported("GL_EXT_copy_image")) {
		bool copy_pass =
			test_copy_image(test, texture, &texture_copy);
		pass &= copy_pass;
		piglit_report_subtest_result(PIGLIT_RESULT(copy_pass),
					     "copy image format 0x%x",
					     test->iformat);
		render_texture(texture_copy, GL_TEXTURE_2D, fbo);
	}

	/* If format can be read, verify contents. */
	if (test->can_read)
		pass &= verify_contents(test);

	/* Render fbo contents to window. */
	render_texture(fbo_tex, GL_TEXTURE_2D, 0);

	piglit_present_results();

	glDeleteFramebuffers(1, &fbo);
	glDeleteTextures(1, &texture);
	glDeleteTextures(1, &texture_copy);

	return pass;
}