コード例 #1
0
ファイル: test.cpp プロジェクト: baskerbill/Core
bool Init()
{
	// Initialize SDL's Video subsystem
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		std::cout << "Failed to init SDL\n";
		return false;
	}

	// Create our window centered at 512x512 resolution
	mainWindow = SDL_CreateWindow(programName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		512, 512, SDL_WINDOW_OPENGL);

	// Check that everything worked out okay
	if (!mainWindow)
	{
		std::cout << "Unable to create window\n";
		CheckSDLError(__LINE__);
		return false;
	}

	// Create our opengl context and attach it to our window
	mainContext = SDL_GL_CreateContext(mainWindow);
	
	SetOpenGLAttributes();

	// This makes our buffer swap syncronized with the monitor's vertical refresh
	SDL_GL_SetSwapInterval(1);

	// Init GLEW
	//glewExperimental = GL_TRUE;
	//glewInit();
	return true;
}
コード例 #2
0
ファイル: persp_sdl_gl.c プロジェクト: rikirenz/Graphic
int main()
{
SDL_Window *win;
SDL_Rect v;
TTF_Font *font;
SDL_Event myevent;
SDL_GLContext mainContext;
int choice, done=0;

if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
	fprintf (stderr, "Couldn't init video: %s\n", SDL_GetError ());
	return (1);
}

/* Initialize the TTF library */
if (TTF_Init () < 0)
{
	fprintf (stderr, "Couldn't initialize TTF: %s\n", SDL_GetError ());
	SDL_Quit ();
	return (2);
}

font = TTF_OpenFont ("FreeSans.ttf", DEFAULT_PTSIZE);
if (font == NULL)
{
	fprintf (stderr, "Couldn't load font\n");
}

v.x = 0;
v.y = 0;
v.w = 720;
v.h = 600;
xvmin = v.x+5;
yvmin = v.y+5;
xvmax = v.w-130;
yvmax = v.h-10;

  win= SDL_CreateWindow("View Bivariate Function", 0, 0, v.w, v.h, SDL_WINDOW_OPENGL);
  if(win==NULL){
        fprintf(stderr,"SDL_CreateWindow Error: %s\n",SDL_GetError());
        SDL_Quit();
        return 1;
  }

SetOpenGLAttributes();
//Create our opengl context and attach it to our window
mainContext=SDL_GL_CreateContext(win);

init_menu(menu);
define_fun_grid();   /* griglia di approssimazione funzione bivariata*/
define_view(); /* calcolo dei parametri di vista */
display(win,menu,font,v);

while (done == 0)
{
 if (SDL_PollEvent(&myevent))
 {
 SDL_EventState(0xff, SDL_IGNORE); //0xff is all events
 switch(myevent.type)
 {
  case SDL_MOUSEMOTION:
    if(myevent.motion.state==1)
    {
       opt_menu(menu,11,myevent.motion.x,myevent.motion.y,&choice);
       switch(choice)
       {
        case 1: //choice==tetawin
  		tmpx = myevent.motion.x-(menu[1].rect.x+menu[1].rect.w/2);
  		tmpy = myevent.motion.y-(menu[1].rect.y+menu[1].rect.h/2);
  		teta = atan2(tmpy,tmpx);
                display(win,menu,font,v);
	 break;
	 case 2: //choice==fiwin 	  
  		tmpx = myevent.motion.x-(menu[2].rect.x+menu[2].rect.w/2);
  		tmpy = myevent.motion.y-(menu[2].rect.y+menu[2].rect.h/2);
  		fi = atan2(tmpy,tmpx);
                display(win,menu,font,v);
         break;
        }
   }
   break;
  
  case SDL_MOUSEBUTTONDOWN:
        if(myevent.button.button==1)
        {
           opt_menu(menu,11,myevent.button.x,myevent.button.y,&choice);
           switch(choice)
           {
            case 0: //choice == finewin 
	        done = 1;
	    break;
	    case 7: //choice == rightwin
                ssx=-sin(teta);
		ssy=cos(teta);
		csx-=sr*ssx;
		csy-=sr*ssy;
                display(win,menu,font,v);
	    break;
	    case 8: //choice == leftwin
		ssx=-sin(teta);
		ssy=cos(teta);
		csx+=sr*ssx;
		csy+=sr*ssy;
                display(win,menu,font,v);
	    break;
	    case 9: //choice == upwin
		ssx=-cos(fi)*cos(teta);
		ssy=-cos(fi)*sin(teta);
		ssz=sin(fi);
		csx+=sr*ssx;
		csy+=sr*ssy;
		csz+=sr*ssz;
                display(win,menu,font,v);
	     break;	
	     case 10: //choice == downwin
		ssx=-cos(fi)*cos(teta);
		ssy=-cos(fi)*sin(teta);
		ssz=sin(fi);
		csx-=sr*ssx;
		csy-=sr*ssy;
		csz-=sr*ssz;
                display(win,menu,font,v);
	     break;
	     case 3: //choice == ziwin
		if (alpha-salpha >0)
  		   alpha-=salpha;
                display(win,menu,font,v);
             break;
	     case 4: //choice == zowin
	        if (alpha+salpha <1.57)
  		   alpha+=salpha;
                display(win,menu,font,v);
             break;
	     case 5: //choice == Dziwin
	        if (D-Dstep>0)
	     	  D-=Dstep;
                display(win,menu,font,v);
             break;
	     case 6: //choice == Dzowin
	     	D+=Dstep;
                display(win,menu,font,v);
             break;
            }
       }
     break;     
  case SDL_KEYDOWN:
    if(myevent.key.keysym.sym == SDLK_ESCAPE)
   	done = 1;
  break; 
  SDL_EventState(0xff, SDL_ENABLE);
  }/* fine switch */ 	
  SDL_EventState(0xff, SDL_ENABLE);
 }
} /* fine while */

TTF_CloseFont (font);
TTF_Quit ();
SDL_GL_DeleteContext(mainContext);
SDL_DestroyWindow(win);
SDL_Quit ();
return (0);

} /* fine main */