「SDLで画像を移動させる」を教えて頂きたい。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
gogo

「SDLで画像を移動させる」を教えて頂きたい。

#1

投稿記事 by gogo » 7年前

正直自分もいろいろよくわかっていなくて、、、。
目的はSDLプログラミングでキーボードを使って画像を移動させることなんですが、アニメーションとかをつかうのか、コールバック関数がいるのか、bufferとかいるのか、どこが間違っていて、追加するものは何かを教えていただければ嬉しいです。あとJOYSTICKのところは無視して頂いて結構です。よろしくお願いします。

コード:

#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

int i,j;
int x = 0;
int y = 0;
int flags1;
SDL_Surface *window; 
SDL_Surface *image;
SDL_Surface *image2;
SDL_Surface *move_image;
SDL_Surface *buffer;
SDL_Event event; // SDLによるイベントを検知するための構造体
SDL_Joystick *joystick;	// ジョイスティックを特定・利用するための構造体
SDL_Event quit_event = {SDL_QUIT};
SDL_TimerID timer_id;	// タイマ割り込みを行うためのタイマのID


//*---------------キャラクタ画像に関する構造体--------------*//
struct Point {
int x;
int y;
int w;
int h;
};
struct SDL{
SDL_Surface *move_image;
struct SDL_Rect src_rect3,dst_rect3;
};
struct SDL fromto;


//*----------------callbackfunc関数の設定-------------------*//

Uint32 callbackfunc(Uint32 interval, void *param){
printf("Callback Function 1 - 1sec Passed¥n");

return interval;
}







//*---------------------Main関数----------------------*//
int main(int argc, char *argv[]){ 

int exit_prg = 0;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_INTERVAL, SDL_DEFAULT_REPEAT_INTERVAL);

if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) { // SDL 初期化
printf("failed to initialize SDL.¥n");
exit(-1);
}

if((window = SDL_SetVideoMode(1000, 550, 32, SDL_SWSURFACE)) == NULL) { // ウィンドウ生成
printf("failed to initialize videomode.¥n");
exit(-1);
}

image = IMG_Load ("forest.jpg");
image2 = IMG_Load ("zonbi1.png");


typedef struct{
Sint16 x, y; // 矩形の左上端の座標と座標
Uint16 w, h; // 矩形の横幅と高さ
} SDL_Rect;

SDL_Rect rect_tmp;
SDL_Rect src_rect = { 0, 0, 1000, 800 }; // 転送元画像の領域を(50,50)-(150,150)に設定
SDL_Rect dst_rect = { 0, 0 }; // 画像の転送先座標を(200, 100)に設定
SDL_Rect src_rect2 = { 0, 0, 150, 300 }; // 転送元画像の領域を(50,50)-(150,150)に設定
SDL_Rect dst_rect2 = { x, y }; // 画像の転送先座標を(100, 200)に設定



buffer = SDL_CreateRGBSurface(SDL_HWSURFACE, 1000, 800, 32, 0, 0, 0, 0);
SDL_BlitSurface(image, &src_rect, SDL_GetVideoSurface(), &dst_rect);
SDL_BlitSurface(image2, &src_rect2, SDL_GetVideoSurface(), &dst_rect2);
timer_id = SDL_AddTimer(1000, callbackfunc, NULL);

//*----------------ゲーム内容設定----------------*//

move_image=IMG_Load("zonbi2.png");	

fromto.src_rect3.x=0;// 画像のコピー先の座標(初期座標)
fromto.src_rect3.y=430;
fromto.dst_rect3.w=70;
fromto.dst_rect3.h=120;

SDL_BlitSurface(move_image, &fromto.dst_rect3, SDL_GetVideoSurface(), &fromto.src_rect3);

//*-------------JOYSTICKを開く&設定-------------*//
for(i=0;i<SDL_NumJoysticks();i++){
printf("%s\n", SDL_JoystickName(i));
}
joystick=SDL_JoystickOpen(0);	
SDL_JoystickEventState(SDL_ENABLE);	


if(!joystick) {
printf("failed to open joystick.\n");
exit(-1);
}


//*---------------キーボードの操作-----------------*//
/* イベントループ */
	while(exit_prg == 0){	
		if(SDL_PollEvent(&event)){
			switch(event.type){
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym){
				case SDLK_UP:
					rect_tmp.y -= 1;
					break;
				case SDLK_DOWN:
					rect_tmp.y += 1;
					break;
				case SDLK_RIGHT:
					rect_tmp.x += 1;
					break;
				case SDLK_LEFT:
					rect_tmp.x -= 1;
					break;
				case SDLK_ESCAPE:
					exit_prg = 1;
					break;
				case SDLK_SPACE:
	
					break;
				default:
					break;
				}

		
		}	
            }
		SDL_Delay(1);
	}

	SDL_FreeSurface(image);

	SDL_Quit();

	return 0;
}


[/color]

“C言語何でも質問掲示板” へ戻る