Example #1
0
static void ProcessGameOver(struct BALL *Ball, struct PADDLE *Paddle)
{
	char OutBuf[255];
	
	snprintf(OutBuf, sizeof OutBuf, "GAME OVER! Score is %d. Hit ESC to exit or space to play again.", Score);

	Lives = 0;
	
	DrawStats();
	
	Lives = 3;
	Score = 0;
	
	DrawMessage(OutBuf);
	
	cbreak();
	
AskAgain:
	switch (getch())
	{
		case 27: /*27 is ESC.*/
			endwin();
			exit(0);
			break;
		case ' ':
			break;
		default:
			goto AskAgain;
	}
	
	halfdelay(1);
	DeleteMessage();
	
	DeleteBall(Ball);
	DeletePaddle(Paddle);
	DeleteAllBricks();
	DeleteAllCharms();
	SetLevel(1); /*Set back to the default level again.*/
	
	ResetPaddle(Paddle);
	ResetBricks();
	
	DrawPaddle(Paddle);
	DrawAllBricks();
	
	DrawStats();
	
	/*Assume they want to play again.*/
	WaitForUserLaunch();
	ResetBall(Ball);
	DrawBall(Ball);
	DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/

}
void RectangleDrawer::DoDraw(wxClientDC& dc, bool isInnerInclude)
{
	dc.Clear();
	DrawBox(dc);
	DrawStats(dc);

	for (size_t i = 0, i_end = m_pRectangleDB->Size(); i < i_end; ++i)
	{
		if ((*m_pRectangleDB)[i] && (*m_pRectangleDB)[i]->GetStatus() == ON_FIELD)
		{
			DrawRectangle(dc, (*m_pRectangleDB)[i], false, isInnerInclude);
		}
	}

	if (m_bDoScreenShot)
	{
		size_t uiCurrentFile = m_pController->GetCurrentTestFile();
		const char* fileName = ConfigReader::GetInstance()->GetTestName(uiCurrentFile).c_str();
		char str[128];
		wxDateTime now = wxDateTime::Now();
		sprintf_s(str, "screenshots/_time_%s_file_%s_conf_%d_repeat_%d_fit_%4.1f.bmp", 
			now.Format("%H.%M.%S", wxDateTime::CEST).c_str(), 
			fileName,
			m_pController->GetCurrentTestConfig()+1,
			m_pController->GetCurrentTestRepeat(),
			m_fFitnress);
		GetScreenShot(dc).SaveFile(str, wxBITMAP_TYPE_BMP);
		m_bDoScreenShot = false;
	}
}
Example #3
0
void byoSnake::OnPaint(wxPaintEvent& event)
{
    wxSize size = GetClientSize();
    wxBitmap buffer(wxImage(size.GetWidth(),size.GetHeight()));
    wxBufferedPaintDC DC(this,buffer);
    DrawBorder(&DC);
    DrawSnake(&DC);
    DrawApple(&DC);
    DrawStats(&DC);
}
Example #4
0
File: gui.cpp Project: paguz/rop
  void RenderGui(){

    DrawOutlines();
    
    DrawMsgLog();
    
    DrawStats();

    io::flip();
    
  }
/****************************************************************\
 *  called from ThreadConsumer to make and register move        *
 *--------------------------------------------------------------*
 *                                                              *
 *  Name: MyMove (ulMyID, ulUser)                               *
 *        ULONG     ulMyID;     id of caller                    *
 *        ULONG     ulUser;     number of the square to "hit"   *
 *                                                              *
 *  Purpose: color a square on the game board 'my' color.       *
 *           update display of number of hits for 'my' thread.  *
 *                                                              *
 *  Usage: called only from ThreadConsumer                      *
 *                                                              *
 *  Method:                                                     *
 *                                                              *
 *  Returns: none                                               *
 *                                                              *
\****************************************************************/
static VOID MyMove (ULONG ulMyID, ULONG ulUser)
{
   if (aSquares [ulUser].usOwner != ulMyID)
   {
      aSquares[ulUser].usOwner = ulMyID;
      thrConsumers[ulMyID].lHits++;
      DrawResource (&aSquares[ulUser].rcl, colors [ulMyID]);
      DrawStats (ulMyID);
   }
   return;
}
Example #6
0
  void TUI::Redraw (int drawFlags)
  {
    // Redraw the "static" background
    if (simpleMode)
    {
      DrawSimple ();
      return;
    }

    // Clear
    if (drawFlags & TUI_DRAW_CLEAR)
      csPrintf (CS_ANSI_CLEAR_SCREEN);

    // Screen layout (keep to 80x25...)
    if (drawFlags & TUI_DRAW_STATIC)
      DrawStatic ();

    // Draw progress
    if (drawFlags & TUI_DRAW_PROGRESS)
      DrawProgress ();

    // Draw messages
    if (drawFlags & TUI_DRAW_MESSAGES)
      DrawMessage ();

    // Draw RayCore stats
    if (drawFlags & TUI_DRAW_RAYCORE)
      DrawRayCore ();

    // Draw photonmapper stats
    if (drawFlags & TUI_DRAW_PMCORE)
      DrawPMCore ();

    // Draw global settings
    if (drawFlags & TUI_DRAW_SETTINGS)
      DrawSettings ();
      
    // Draw global stats
    if (drawFlags & TUI_DRAW_STATS)
      DrawStats ();
      
    if (drawFlags & TUI_DRAW_SWAPCACHE)
      DrawSwapCacheStats ();

    /* Linux: output is buffered, and the UI may appear "incomplete" if not 
     * flushed */
    fflush (stdout);
  }
Example #7
0
void
yawmppp_routine (int argc, char **argv)
{
    int i, j;

    int but_stat;

    long currenttime;
    long lasttime;
    long waittime;
    long ppptime;
    int hour, minute;
    long timetolog;

    long ppp_send, ppp_sl = -1;
    long ppp_recv, ppp_rl = -1;
    long ppp_sbytes, ppp_rbytes;
    long ppp_osbytes, ppp_orbytes;

    struct stat st;

    pid_t stop_child = 0;
    pid_t start_child = 0;
    int status;

    int isonline = 0;

    XEvent Event;

    int speed_ind = 10;

    /* Initialize some stuff */

    get_statistics (active_interface, &ppp_rl, &ppp_sl,
		    &ppp_orbytes, &ppp_osbytes);
    if (caution>0)
      close_ppp();

    grab_isp_info(1);

    /* Open the display */

    createXBMfromXPM (dock_mask_bits, dockxpm_xpm,
		      dock_mask_width, dock_mask_height);

    openXwindow (argc, argv, dockxpm_xpm, dock_mask_bits,
		 dock_mask_width, dock_mask_height);

    /* V Button */
    AddMouseRegion (0, 35, 48, 46, 58);
    /* x Button */
    AddMouseRegion (1, 47, 48, 58, 58);

    /* < Button */
    AddMouseRegion (2, 5, 48, 16, 58);
    /* > Button */
    AddMouseRegion (3, 17, 48, 28, 58);
    /* ISP display */
    AddMouseRegion (4, 5, 6, 59, 43);

    starttime = 0;
    currenttime = time (0);
    ppptime = 0;
    but_stat = -1;
    waittime = 0;
    timetolog=0;

    /* 888k8 on bottom */
    copyXPMArea (ERR_SRC_X+28, ERR_SRC_Y+9, 25, 8, ERR_DEST_X, ERR_DEST_Y);

    DrawISPName ();

    while (1)
      {
	  lasttime = currenttime;
	  currenttime = time (0);

	  /* Check if any child has left the playground */
	  i = waitpid (0, &status, WNOHANG);
	  if (i == stop_child && stop_child != 0)
	    {

		starttime = 0;
		SetOffLED (LED_PPP_POWER);
		SetOffLED (LED_PPP_RX);
		SetOffLED (LED_PPP_TX);
		/* 888k8 on bottom */
		copyXPMArea (ERR_SRC_X+28, ERR_SRC_Y+9, 25, 8,
			     ERR_DEST_X, ERR_DEST_Y);
		RedrawWindow ();

		stop_child = 0;
	    }
	  if (i == start_child && start_child != 0)
	    {
		if (WIFEXITED (status))
		  {
		      if (WEXITSTATUS (status) == 10)
			{

			    starttime = 0;
			    /* 88k8 on bottom */
			    copyXPMArea (ERR_SRC_X+28, ERR_SRC_Y+9, 25, 8,
					 ERR_DEST_X, ERR_DEST_Y);
			    SetOffLED (LED_PPP_POWER);
			    DrawTime (0, 1);
			    RedrawWindow ();
			}
		      start_child = 0;
		  }
	    }

	  /* On-line detectie! 1x per second */

	  if (currenttime != lasttime)
	    {
		i = 0;

		if (stillonline (active_interface))
		  {
		      i = 1;
		      if (!starttime)
			{
			    starttime = currenttime;

			    if (stat (STAMP_FILE, &st) == 0)
				starttime = st.st_mtime;

			    SetOnLED (LED_PPP_POWER);
			    waittime = 0;

			    /* 88k8 on bottom */
			    copyXPMArea (ERR_SRC_X+28, ERR_SRC_Y+9, 25, 8,
					 ERR_DEST_X, ERR_DEST_Y);

			    if (IspData[current_isp].SpeedAction)
				DrawSpeedInd (IspData[current_isp].SpeedAction);

			    speed_ind = currenttime + 10;

			    RedrawWindow ();
			}
		  }
		if (!i && starttime)
		  {
		      starttime = 0;
		      SetErrLED (LED_PPP_POWER);
		      logconn.status=1;

		      /* Error */
		      copyXPMArea (ERR_SRC_X, ERR_SRC_Y+9, 25, 8,
				   ERR_DEST_X, ERR_DEST_Y);

		      if (IspData[current_isp].IfDownAction)
			  execCommand (IspData[current_isp].IfDownAction);

		      RedrawWindow ();
		  }
	    }

	  if (waittime && waittime <= currenttime)
	    {
		SetOffLED (LED_PPP_POWER);
		RedrawWindow ();
		waittime = 0;
	    }

	  if ((starttime)&&(!isonline)) {
	    isonline=1;

	    logconn.start=time(NULL);
	    logconn.status=0;
	    strcpy(logconn.longname,IspData[current_isp].LongName);
	    strcpy(logconn.shortname,IspData[current_isp].ShortName);
	    strcpy(logconn.user,IspData[current_isp].User);
	    strcpy(logconn.phone,IspData[current_isp].Phone);

	    if (!strlen(logconn.shortname))
	      strcpy(logconn.shortname,"empty");
	    if (!strlen(logconn.longname))
	      strcpy(logconn.longname,"empty");
	    if (!strlen(logconn.user))
	      strcpy(logconn.user,"empty");
	    if (!strlen(logconn.phone))
	      strcpy(logconn.phone,"empty");

	    make_guards();
	  }
	  if ((!starttime)&&(isonline)) {
	    isonline=0;
	    logconn.end=time(NULL);
	    write_log();
	    if (got_sched)
	      make_delayed_update();
	    if (caution>0)
	      close_ppp();
	  }

	  /* If we are on-line. Print the time we are */
	  if (starttime)
	    {
		i = currenttime - starttime;

		i /= TimerDivisor;

		if (TimerDivisor == 1)
		    if (i > 59 * 60 + 59)
			i /= 60;

		minute = i % 60;
		hour = (i / 60) % 100;
		i = hour * 100 + minute;

		DrawTime (i, currenttime % 2);
		/* We are online, so we can check for send/recv packets */

		get_statistics (active_interface, &ppp_recv, &ppp_send,
				&ppp_rbytes, &ppp_sbytes);
		if (caution>1)
		  close_ppp();

		if (ppp_send != ppp_sl)
		    SetOnLED (LED_PPP_TX);
		else
		    SetOffLED (LED_PPP_TX);

		if (ppp_recv != ppp_rl)
		    SetOnLED (LED_PPP_RX);
		else
		    SetOffLED (LED_PPP_RX);

		ppp_sl = ppp_send;
		ppp_rl = ppp_recv;

		/* Every five seconds we check to load on the line */

		if (currenttime - timetolog >= 0) {
		  timetolog=currenttime + 60;
		  make_guards();
		}

		if ((currenttime - ppptime >= 0) || (ppptime == 0))
		  {

		      ppptime = currenttime + updaterate;

		      ppp_history[PPP_STATS_HIS][0] = ppp_rbytes - ppp_orbytes;
		      ppp_history[PPP_STATS_HIS][1] = ppp_sbytes - ppp_osbytes;

		      ppp_orbytes = ppp_rbytes;
		      ppp_osbytes = ppp_sbytes;

		      DrawStats (54, 17, 5, 32);

		      for (j = 1; j < 55; j++)
			{
			    ppp_history[j - 1][0] = ppp_history[j][0];
			    ppp_history[j - 1][1] = ppp_history[j][1];
			}
		      if (currenttime > speed_ind)
			{
			    DrawLoadInd ((ppp_history[54][0] + ppp_history[54][1]) / updaterate);
			}
		  }

		RedrawWindow ();
	    }


	  while (XPending (display))
	    {
		XNextEvent (display, &Event);
		switch (Event.type)
		  {
		  case Expose:
		      RedrawWindow ();
		      break;
		  case DestroyNotify:
		      XCloseDisplay (display);
		      while (start_child | stop_child)
			{
			    i = waitpid (0, &status, WNOHANG);
			    if (i == stop_child)
				stop_child = 0;
			    if (i == start_child)
				start_child = 0;
			    usleep (50000l);
			}
		      exit (0);
		      break;
		  case ButtonPress:
		      i = CheckMouseRegion (Event.xbutton.x, Event.xbutton.y);
		      switch (i)
			{
			case 0:
			    ButtonDown (BUT_V);
			    break;
			case 1:
			    ButtonDown (BUT_X);
			    break;
			case 2:
			    ButtonDown (BUT_REW);
			    break;
			case 3:
			    ButtonDown (BUT_FF);
			    break;
			}
		      but_stat = i;

		      RedrawWindow ();
		      break;
		  case ButtonRelease:
		      i = CheckMouseRegion (Event.xbutton.x, Event.xbutton.y);
		      // Button but_stat omhoogdoen!
		      switch (but_stat)
			{
			case 0:
			    ButtonUp (BUT_V);
			    break;
			case 1:
			    ButtonUp (BUT_X);
			    break;
			case 2:
			    ButtonUp (BUT_REW);
			    break;
			case 3:
			    ButtonUp (BUT_FF);
			    break;
			}

		      if (i == but_stat && but_stat >= 0)
			{
			    switch (i)
			      {
			      case 0:
				  if (!starttime)
				    {
				      /* 888k8 */
				      copyXPMArea (ERR_SRC_X+28, ERR_SRC_Y+9,
						   25, 8,
						   ERR_DEST_X, ERR_DEST_Y);
					DrawTime (0, 1);
					start_child = execCommand (IspData[current_isp].StartAction);
					SetWaitLED (LED_PPP_POWER);
					waittime = ORANGE_LED_TIMEOUT + currenttime;
				    }

				  break;
			      case 1:
				  if (stop_child == 0)
				    {
					stop_child = execCommand (IspData[current_isp].StopAction);
				    }
				  break;
			      case 2:
				  if (!starttime)
				    {
					current_isp--;
					if (current_isp < 0)
					    current_isp = num_isps - 1;
					if (current_isp < 0)
					  current_isp=0;
					DrawISPName ();
				    }
				  break;
			      case 3:
				  if (!starttime)
				    {
					current_isp++;
					if (current_isp == num_isps)
					    current_isp = 0;
					DrawISPName ();
				    }
				  break;
			      case 4:
				if (Event.xbutton.button==Button1)
				  run_pref_app();
				else
				  run_log_app();
				break;
			      }
			}
		      RedrawWindow ();

		      but_stat = -1;
		      break;
		  default:
		      break;
		  }
	    }
	  usleep (50000L);
      }
}
Example #8
0
void wmifs_routine(int argc, char **argv)
{

	rckeys	wmifs_keys[] = {
		{ "left", &left_action },
		{ "middle", &middle_action },
		{ "right", &right_action },
		{ NULL, NULL }
	};


	int			i, j;
	XEvent		Event;
	int			but_stat = -1;

	int			stat_online;
	int			stat_current;
	int			first_time = 1;

	unsigned int	curtime;
	unsigned int	nexttime;
	struct timeval	tv, tv2;

	long		ipacket, opacket, istat, ostat;

	char		temp[BUFFER_SIZE];
	char		*p;

	for (i = 0; i < MAX_STAT_DEVICES; i++) {
		stat_devices[i].name[0] = 0;
		for (j = 0; j < 48; j++) {
			stat_devices[i].his[j][0] = 0;
			stat_devices[i].his[j][1] = 0;
		}
	}

	stat_online = checknetdevs();

	stat_current = 0;
	if (active_interface) {
		int isauto = !strcmp(active_interface, "auto");
		for (i = 0; i < stat_online; i++) {
			if ((isauto && stillonline(stat_devices[i].name)) ||
			    !strcmp(stat_devices[i].name, active_interface)) {
				stat_current = i;
				break;
			}
		}
	}

#ifdef LEFT_ACTION
	left_action = strdup(LEFT_ACTION);
#endif
#ifdef MIDDLE_ACTION
	middle_action = strdup(MIDDLE_ACTION);
#endif
#ifdef RIGHT_ACTION
	right_action = strdup(RIGHT_ACTION);
#endif

	/* Scan throught the .rc files */
	parse_rcfile(CONF"/wmifsrc", wmifs_keys);

	p = getenv("HOME");
	if (p == NULL || *p == 0) {
		fprintf(stderr, "Unknown $HOME directory, please check your environment\n");
		return;
	}
	strncpy(temp, p, BUFFER_SIZE - 10);
	strcat(temp, "/.wmifsrc");
	parse_rcfile(temp, wmifs_keys);

	parse_rcfile(CONF"/wmifsrc.fixed", wmifs_keys);

       /* set user-defined colors */
       if (color[0] != 0) {
               Window  Root;
               XColor col;
               XWindowAttributes attributes;
               int screen;
               Pixel pixel;
#define NUMSYMBOLS 4
               XpmColorSymbol user_color[NUMSYMBOLS] = {
                       {NULL, "#2081B2CAAEBA", 0}, /* + */
                       {NULL, "#28A23CF338E3", 0}, /* O */
                       {NULL, "#000049244103", 0}, /* @ */
                       {NULL, "#18618A288617", 0}, /* # */
                        };


               /* code based on GetColor() from wmgeneral.c */
               /* we need a temporary display to parse the color */
               display = XOpenDisplay(NULL);
               screen = DefaultScreen(display);
               Root = RootWindow(display, screen);
               XGetWindowAttributes(display, Root, &attributes);

               col.pixel = 0;
               if (!XParseColor(display, attributes.colormap, color, &col)) {
                       fprintf(stderr, "wmtime: can't parse %s.\n", color);
                       goto draw_window;
               } else if (!XAllocColor(display, attributes.colormap, &col)) {
                       fprintf(stderr, "wmtime: can't allocate %s.\n", color);
                       goto draw_window;
               }

               pixel = col.pixel;

               /* replace colors from wmtime-master.xpm */
               user_color[0].pixel = pixel;
               user_color[1].pixel = scale_pixel(pixel, .3);
               user_color[2].pixel = scale_pixel(pixel, .4);
               user_color[3].pixel = scale_pixel(pixel, .8);

               wmgen.attributes.valuemask |= XpmColorSymbols;
               wmgen.attributes.numsymbols = NUMSYMBOLS;
               wmgen.attributes.colorsymbols = user_color;

               XCloseDisplay(display);
       }

draw_window:
	openXwindow(argc, argv, wmifs_master_xpm, (char*)wmifs_mask_bits, wmifs_mask_width, wmifs_mask_height);

	/* > Button */
	AddMouseRegion(0, 5, 5, 35, 15);
	AddMouseRegion(1, 5, 20, 58, 58);

	gettimeofday(&tv2, NULL);
	nexttime = ScrollSpeed;

	DrawActiveIFS(stat_devices[stat_current].name);

	while (1) {
		struct timespec ts;

		gettimeofday(&tv, NULL);
		curtime = (tv.tv_sec - tv2.tv_sec) * 1000
			+ (tv.tv_usec - tv2.tv_usec) / 1000;

		waitpid(0, NULL, WNOHANG);

		for (i = 0; i < stat_online; i++) {
			get_statistics(stat_devices[i].name, &ipacket, &opacket, &istat, &ostat);

			if (first_time) {
				first_time = 0;
			} else {
				stat_devices[i].his[53][0] += istat - stat_devices[i].istatlast;
				stat_devices[i].his[53][1] += ostat - stat_devices[i].ostatlast;
			}

			if (i == stat_current) {
				if (!stillonline(stat_devices[i].name))
					SetErrLED(LED_NET_POWER);
				else
					SetOnLED(LED_NET_POWER);

				if (stat_devices[i].istatlast == istat)
					SetOffLED(LED_NET_RX);
				else
					SetOnLED(LED_NET_RX);

				if (stat_devices[i].ostatlast == ostat)
					SetOffLED(LED_NET_TX);
				else
					SetOnLED(LED_NET_TX);
			}

			stat_devices[i].istatlast = istat;
			stat_devices[i].ostatlast = ostat;
		}
		RedrawWindow();

		if (curtime >= nexttime) {
			nexttime = curtime + ScrollSpeed;

			DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
			for (i = 0; i < stat_online; i++) {
				if (stillonline(stat_devices[i].name)) {
					for (j = 1; j < 54; j++) {
						stat_devices[i].his[j-1][0] = stat_devices[i].his[j][0];
						stat_devices[i].his[j-1][1] = stat_devices[i].his[j][1];
					}
					stat_devices[i].his[53][0] = 0;
					stat_devices[i].his[53][1] = 0;
				}
			}
			RedrawWindow();
		}

		while (XPending(display)) {
			XNextEvent(display, &Event);
			switch (Event.type) {
			case Expose:
				RedrawWindow();
				break;
			case DestroyNotify:
				XCloseDisplay(display);
				exit(0);
				break;
			case ButtonPress:
				but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
				break;
			case ButtonRelease:
				i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);

				if (but_stat == i && but_stat >= 0) {
					switch (but_stat) {
					case 0:
						/* re-read the table */
						strcpy(temp, stat_devices[stat_current].name);
						stat_online = checknetdevs();
						stat_current = 0;
						for (i = 0; i < stat_online; i++) {
							if (!strcmp(temp, stat_devices[i].name))
								stat_current = i;
						}

						stat_current++;
						if (stat_current == stat_online)
							stat_current = 0;

						DrawActiveIFS(stat_devices[stat_current].name);

						DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
						break;
					case 1:
						switch (Event.xbutton.button) {
						case 1:
							if (left_action)
								execCommand(left_action);
							break;
						case 2:
							if (middle_action)
								execCommand(middle_action);
							break;
						case 3:
							if (right_action)
								execCommand(right_action);
							break;
						}
						break;

					}
				}
				but_stat = -1;
				RedrawWindow();
				break;
			}
		}
		ts.tv_sec = 0;
		ts.tv_nsec = SampleInt * 1000000;
		nanosleep(&ts, NULL);
	}
}
Example #9
0
static void GameLoop(struct BALL *const Ball, struct PADDLE *const Paddle)
{ /*Primary loop where most events get processed.*/
	
	struct BRICKSTRIKE Strike;
	int Key = 0;
	int SecTick = 0;
	Bool PaddleMovedLastTick;
	DirectionX PaddleMoveDir;
	int Inc = 0;
	Bool Flip = false;
	int SlowBallTicks = 0, BallNukeTicks = 0;
	
	while ((Key = getch()) != 27) /*27 is ESC*/
	{		
		if (SecTick == 10)
		{ /*We get score every second for just surviving.*/
			Score += 2;
			DrawStats();
			SecTick = 0;
		}
		++SecTick;

		if (Ball->Y == 1)
		{ /*We hit the ceiling.*/
			BounceBallY(Ball, DOWN);
		}
		else if (Ball->Y >= BRICKTICK_MAX_Y - 2)
		{ /*More happens when we hit the floor.*/
			if (!CheckBallHitPaddle(Ball, Paddle))
			{
				DeleteBall(Ball);
				Ball->Y = BRICKTICK_MAX_Y - 1;
				DrawBall(Ball);
				
				if (Lives == 1)
				{ /*We ran out of lives.*/
					ProcessGameOver(Ball, Paddle);
				}
				else
				{
					--Lives;
					DrawStats();
					
					WaitForUserLaunch();
							
					DeleteBall(Ball);
					DeletePaddle(Paddle);
					
					ResetBall(Ball);
					ResetPaddle(Paddle);
					
					DrawPaddle(Paddle);
					DrawBall(Ball);
					
					/*Redraw but don't reset.*/
					DrawAllBricks();
				}
			}
			else
			{
				BounceBallY(Ball, UP);
				
				if (PaddleMovedLastTick)
				{ /*We can "whack" the ball with our Paddle->*/
					Ball->DirX = PaddleMoveDir;
				}
				else
				{ /*We cut the paddle into thirds for the X direction after bounce.*/
#define PADDLE_THIRD (Paddle->Length / 3)
					if (Ball->X <= Paddle->X + PADDLE_THIRD)
					{
						Ball->DirX = LEFT;
					}
					else if (Ball->X  > Paddle->X + PADDLE_THIRD && Ball->X <= Paddle->X + (PADDLE_THIRD * 2))
					{
						/*Make whether we hit up or not as a chance.*/
						Bool StraightUp = rand() & 1;
						if (StraightUp) Ball->DirX = X_NEUTRAL;
					}
					else
					{
						Ball->DirX = RIGHT;
					}
				}
			}
		}
		PaddleMovedLastTick = false;
		/*Bounce off left and right walls.*/
		if (Ball->X >= BRICKTICK_MAX_X - 1)
		{
			Ball->X = BRICKTICK_MAX_X - 1;
			BounceBallX(Ball, LEFT);
		}
		else if (Ball->X <= 0)
		{
			Ball->X = 0;
			BounceBallX(Ball, RIGHT);
		}
			
	
		/*Check if a charm hit the paddle.*/
		for (Inc = 0; Inc < BRICK_MAX_NUMLINES * BRICK_MAX_PERLINE; ++Inc)
		{
			if (Charms[Inc].Type == CHARM_NONE || !Charms[Inc].Dropped || Charms[Inc].Y != BRICKTICK_MAX_Y - 2) continue;
			
			if (CheckCharmHitPaddle(Paddle, Charms + Inc))
			{
				void *Ptr = NULL;
				const char *const Strings[] = { "+1,000 Score", "+1 Lives",
											"10 Second Slow Ball", "3 second nuke mode" };
					
				switch (Charms[Inc].Type)
				{
					case CHARM_SCORE:
						Ptr = &Score;
						break;
					case CHARM_LIFE:
						Ptr = &Lives;
						break;
					case CHARM_SLOW:
						Ptr = &SlowBallTicks;
						break;
					case CHARM_NUKE:
						Ptr = &BallNukeTicks;
						break;
					default:
						break;
				}
				
				if (Charms[Inc].Type != CHARM_NONE)
				{ /*Show a message on what type of charm we have here.*/
					DrawMessage(Strings[Charms[Inc].Type - 1]); fflush(NULL);
					usleep(500000);
					DeleteMessage();
					DrawAllBricks();
					DrawStats();
				}
				
				/*Do the thing the charm does.*/
				ProcessCharmAction(Charms + Inc, Ptr);
			}
			
			/*In any case, we're done with it.*/
			DeleteCharm(Charms + Inc);
			Charms[Inc].Type = CHARM_NONE;
		}
		
		/*We hit a brick.*/
		if (BallStruckBrick(Ball, &Strike))
		{
			
			if (BallNukeTicks == 0) /*Nuclear ball passes through.*/
			{
				switch (Strike.StrikeV)
				{
					case STRIKE_TOP:
						Ball->DirY = UP;
						break;
					case STRIKE_BOTTOM:
						Ball->DirY = DOWN;
						break;
					default:
						break;
				}
				
				switch (Strike.StrikeH)
				{
					case STRIKE_LEFT:
						Ball->DirX = LEFT;
						break;
					case STRIKE_RIGHT:
						Ball->DirX = RIGHT;
						break;
					default:
					{
						if (Ball->DirX != X_NEUTRAL) break;
						else
						{
							Bool Dir = rand() & 1;
							Ball->DirX = (DirectionX)Dir;
						}
						break;
					}
				}
			}
			
			DeleteBrick(Strike.Brick);
			Score += 100;
			DrawStats();
			
			if (!BricksLeft())
			{ /*Move to next level.*/

				if (SetLevel(Level + 1))
				{ /*We have more levels to go before we win.*/
					DeleteAllBricks();
					DeleteAllCharms();
					DeleteBall(Ball);
					DeletePaddle(Paddle);
					DrawStats();
					
					Score += 1000; /*Reward for making it this far.*/
					Lives = BRICKTICK_NUMLIVES;
					
					ResetBall(Ball);
					ResetPaddle(Paddle);
					ResetBricks();
					
					DrawAllBricks();
					DrawPaddle(Paddle);
					WaitForUserLaunch();
					DrawBall(Ball);
					DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/
				}
				else
				{ /*WE WON!!!!*/
					char WonBuf[256];
					
					snprintf(WonBuf, sizeof WonBuf, "You Won! Score is %d! Hit ESC to exit or space to play again.", Score);
					DrawMessage(WonBuf);
					
				WinRegetch:
					switch (getch())
					{
						case 27: /*27 is ESC*/
							endwin();
							exit(0);
							break;
						case ' ':
						{
							DeleteMessage();
							
							SetLevel(1);
							
							Lives = BRICKTICK_NUMLIVES;
							Score = 0;
							
							DeleteAllBricks();
							DeleteAllCharms();
							DeleteBall(Ball);
							DeletePaddle(Paddle);
							DrawStats();
							
							ResetBall(Ball);
							ResetPaddle(Paddle);
							ResetBricks();
							
							DrawAllBricks();
							DrawPaddle(Paddle);
							WaitForUserLaunch();
							DrawBall(Ball);
							DrawAllBricks(); /*Redraw to fix WaitForUserLaunch() goofing.*/

							continue; /*For the loop we are in.*/
						}
						default:
							goto WinRegetch;
					}
				}

				continue;
			}
			else
			{ /*Charm drops.*/
				struct CHARM *Charm = GetCharmByBrick(Strike.Brick);
				
				if (Charm)
				{ /*We DO have a charm for this brick.*/
					PerformCharmDrop(Charm); /*Mark it dropped.*/

					/*Now draw the charm.*/
					DrawCharm(Charm);
				}
			}
		}
		
		switch (Key)
		{ /*Paddle movement.*/
			case KEY_LEFT:
				MovePaddle(Paddle, LEFT);
				PaddleMovedLastTick = true;
				PaddleMoveDir = LEFT;
				break;
			case KEY_RIGHT:
				MovePaddle(Paddle, RIGHT);
				PaddleMovedLastTick = true;
				PaddleMoveDir = RIGHT;
				break;
			case 's': /*They want to save the game.*/
				if (SaveGame(Ball, Paddle))
				{
					DrawMessage("Game saved.");
				}
				else
				{
					DrawMessage("Failed to save game.");
				}
				fflush(NULL);
				usleep(500000);
				
				DeleteMessage();
				DrawAllBricks(); /*Redraw bricks if damaged.*/
				
				break;
			case 'o': /*They want us to load a game.*/
			{
				const Bool LoadedOk = LoadGame(Ball, Paddle);
				
				if (LoadedOk)
				{
					/*Restore the state.*/
					clear();
					DrawBorders(); /*Need to redraw these after clearing the screen.*/
					
					DrawBall(Ball);
					DrawPaddle(Paddle);
					DrawStats();
					DrawAllBricks();
					
					DrawMessage("Game loaded.");
				}
				else
				{
					DrawMessage("Failed to load game.");
				}

				fflush(NULL);
				usleep(500000);
				
				DeleteMessage();
				DrawAllBricks();
				break;
			}
			case ' ':
			{
				DrawMessage("PAUSED");
				cbreak();
				
			PauseRegetch:
				switch (getch())
				{
					case ' ':
						DeleteMessage();
						DrawAllBricks(); /*Redraw to fix what deleting the message messed up.*/
						halfdelay(1);
						break;
					case 27: /*27 is ESC*/
						endwin();
						exit(0);
						break;
					default:
						goto PauseRegetch;
				}
			}
			default:
				break;
		}
		
		Flip = !Flip;

		/*Ball movement, obviously. Flip is used to keep it at half speed if we got a 'slow' charm.*/
		if (Flip || SlowBallTicks == 0) MoveBall(Ball);
		
		/*Decrement slow ball ticks until zero, then the ball goes fast again.*/
		if (SlowBallTicks > 0) --SlowBallTicks;
		
		/*Decrement nuclear ball until ticks hit zero.*/
		if (BallNukeTicks > 0) --BallNukeTicks;
		
		/*Charm movement.*/		
		for (Inc = 0; Inc < BRICK_MAX_NUMLINES * BRICK_MAX_PERLINE; ++Inc)
		{
			if (Charms[Inc].Type == CHARM_NONE || !Charms[Inc].Dropped) continue;
			
			if (Flip) MoveCharm(Charms + Inc);
		}
		
		/*Redraw borders in case of terminal resize.*/
		DrawBorders();
	}
}
Example #10
0
int main(int argc, char **argv)
{
	int Inc = 1;
	struct BALL Ball = { 0 };
	struct PADDLE Paddle = { 0 };
	Bool DoLoadGame = false;
	int GotoLevel = 0;
	
	for (; Inc < argc; ++Inc)
	{ /*Argument parsing.*/
		if (!strcmp("--nocolor", argv[Inc]))
		{
			UseColor = false;
		}
		else if (!strcmp("--version", argv[Inc]))
		{
			printf("Bricktick brick breaker v" BRICKTICK_VERSION "\n"
					"By Subsentient.\n");
			fflush(NULL);
			exit(0);
		}
		else if (!strcmp("--help", argv[Inc]))
		{
			printf("Command line options:\n\t"
					"--help: Show this help.\n\t"
					"--nocolor: Run Bricktick with no color.\n\t"
					"--version: Display version and exit.\n");
			fflush(NULL);
			exit(0);
		}
		else if (!strncmp("--level=", argv[Inc], sizeof "--level=" - 1))
		{
			GotoLevel = atoi(argv[Inc] + sizeof "--level=" - 1);
			
			if (GotoLevel > sizeof Levels / sizeof *Levels)
			{
				fprintf(stderr, "The maximum level is %d.\n", sizeof Levels / sizeof *Levels); fflush(NULL);
				exit(1);
			}
		}
		else
		{
			fprintf(stderr, "Bad command line argument \"%s\".\n"
					"Use --help for command line options.\n", argv[Inc]);
			exit(1);
		}
	}
	
	
	initscr(); /*Fire up ncurses.*/
	
	if (COLS < BRICKTICK_MAX_X || LINES < BRICKTICK_MAX_Y)
	{
		endwin();
		fprintf(stderr, "Please use a console with a resolution of at least %dx%d.\n", BRICKTICK_MAX_X, BRICKTICK_MAX_Y);
		exit(1);
		
	}
	
	/*Color not supported.*/
	if (!has_colors()) UseColor = false;
	
	/*Various ncurses things.*/
	noecho();
	
	if (UseColor)
	{ /*Color fireup.*/
		start_color();
		init_pair(1, COLOR_CYAN, COLOR_BLACK);
		init_pair(2, COLOR_GREEN, COLOR_BLACK);
		init_pair(3, COLOR_BLACK, COLOR_WHITE);
		init_pair(4, COLOR_BLUE, COLOR_BLACK);
		init_pair(5, COLOR_GREEN, COLOR_BLACK);
		init_pair(6, COLOR_YELLOW, COLOR_BLACK);
		init_pair(7, COLOR_RED, COLOR_BLACK);
		
		
		/*Fire up colors for the bricks.*/
		init_pair(BRICK_COLORS_START, COLOR_GREEN, COLOR_BLACK);
		init_pair(BRICK_COLORS_START + 1, COLOR_BLUE, COLOR_BLACK);
		init_pair(BRICK_COLORS_START + 2, COLOR_YELLOW, COLOR_BLACK);
		init_pair(BRICK_COLORS_START + 3, COLOR_MAGENTA, COLOR_BLACK);
		init_pair(BRICK_COLORS_START + 4, COLOR_CYAN, COLOR_BLACK);
		init_pair(BRICK_COLORS_END, COLOR_RED, COLOR_BLACK);
	}
	
	/*Draw borders for consoles bigger than our 80x24 playing area.*/
	DrawBorders();
	
	/*Greeting.*/
GreetAsk:

	if (!GotoLevel)
	{ /*If they specified a level let them go straigth to game start.*/
		DrawGreeting();
		switch (getch())
		{
			case 27: /*27 is ESC key*/
				endwin();
				exit(0);
				break;
			case ' ':
				clear(); /*Wipe the message from the screen.*/
				DrawBorders(); /*Redraw borders.*/
				break;
			case 'o':
				clear();
				DrawBorders();
				DoLoadGame = true;
				LoadGame(&Ball, &Paddle);
				break;
			default:
				goto GreetAsk;
		}
	}
	else
	{
		clear();
		DrawBorders();
	}
	
	halfdelay(1);
	keypad(stdscr, true);
#if NCURSES_VERSION_MAJOR >= 5 && NCURSES_VERSION_MINOR >=4
	set_escdelay(25);
#endif
	curs_set(0);
	

	if (DoLoadGame)
	{
		DrawStats();
		DrawBall(&Ball);
		DrawPaddle(&Paddle);
		DrawAllBricks();
	}
	else
	{
		/*Show our initial lives count.*/
		DrawStats();
	
		/*Reset to level 1 if we haven't specified a level.*/
		SetLevel(GotoLevel ? GotoLevel : 1);
		
		ResetBall(&Ball);
		ResetPaddle(&Paddle);
		
		DrawBall(&Ball);
		DrawPaddle(&Paddle);
		ResetBricks();
		DrawAllBricks();
	}
	
	/*Wait for L key.*/
	WaitForUserLaunch();
	DrawAllBricks(); /*Redraw to fix goofing by WaitForUserLaunch().*/
	
	/*Set up the number generator.*/
	srand(time(NULL));

	/**
	 * Main loop for the game!
	 **/
Reloop:
	GameLoop(&Ball, &Paddle);
	
	DrawMessage("Really quit Bricktick? y/n");
	cbreak();
	
	/*When the game loop exits.*/
	switch (getch())
	{
		case 'y':
		case 'Y':
			break;
		default:
			DeleteMessage();
			refresh();
			halfdelay(1);
			DrawAllBricks(); /*Fix damage to brick display resulting from the message.*/
			goto Reloop;
			break;
	}
	DeleteBall(&Ball);
	DeletePaddle(&Paddle);
	
	endwin();
	
	return 0;
}
Example #11
0
void GamePanel::Update() {
  DrawStats();
}