Solved

c++/cygwin

Posted on 2007-04-11
2
554 Views
Last Modified: 2013-11-15
hi..below is my program..i need help modifying it..im using XWindow on linux..i need help on:

- draw graphics on the window
- report mouse movement
- report mouse-button presses
- report keyboard presses

Draw graphic...my program can already draw graphic..the window pops up and i can draw graphic by touching my mouse and putting the cursor over the screen..my goal is not to touch the mouse and let the window pop up and pixels color the window by itself..it fills up the screen by itself without having me to touch the mouse..if i touch the mouse and press button..i should see on my screen that "you pressed the left button" like that...

 #include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "Xwindow.h"
                                                                               
using namespace std;
                                                                               
#define  bsize       10          // The pixel size of the smiley-face image
#define  MaxXCoord   50         // Width of the window = MaxXCoord * bsize
#define  MaxYCoord   50         // Height of the window = MaxYCoord * bsize
                                                                               
                                                                               
// This is the smiley-face design.
//  0's are skipped, 1's are drawn in whatever color
const int color_array[] = {0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
                           0, 1, 1, 0, 0, 0, 0, 1, 1, 0,
                           0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
                           1, 0, 0, 1, 0, 1, 0, 0, 0, 1,
                           1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                           1, 0, 1, 0, 0, 0, 0, 1, 0, 1,
                           1, 0, 0, 1, 1, 1, 1, 0, 0, 1,
                           1, 1, 0, 0, 0, 0, 0, 0, 1, 1,
                           0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
                           0, 0, 1, 1, 1, 1, 1, 1, 0, 0};
                                                                               
//************************************************************************
//    Random Number Generator functions
//************************************************************************
                                                                               
// Generate a random number from 0 to size-1
unsigned long rand_long(unsigned long size)
{
   float r;
  unsigned long r2;
                                                                               
   r = rand();
   r = ( r / RAND_MAX)*size;
   r2 = (unsigned long) r;
   return( r2 );
}
                                                                               
                                                                               
// Return a number that is >= lowlimit, but < highlimit.
// Work with integers
int rand_int(int lowlimit, int highlimit)
{
   int j,r,limit;
                                                                               
   j = rand();
   limit = highlimit - lowlimit;
   r = 0;
   if (limit!=0)
      r = j % limit;
  r = r + lowlimit;
   return r;
}
                                                                               
                                                                               
// Return a number that is >= lowlimit, but < highlimit.
// Work with unsigned long ints
unsigned long rand_long(unsigned long lowlimit, unsigned long highlimit)
{
   unsigned long j,r,limit;
   j = rand();
   limit = highlimit - lowlimit;
   r = 0;
   if (limit!=0)
      r = j % limit;
   r = r + lowlimit;
   return r;
}
//************************************************************************
//       Main
//************************************************************************
int main(int argc, char *argv[])
{
                                                                               
   Bool quit = false;
                                                                               
   // Initialize rnd generator
 int j = (__TIME__[6]-48) + (__TIME__[7]-48)*10;
   srand(j);
                                                                               
   Xwindow mywindow;
   Xwindow mywindow2;
                                                                               
   int x, y;
   unsigned long  TempX, TempY, TempC;
 // Set up the Xwindow.
   mywindow.Set_Background(mywindow.BLACK());
   mywindow.Set_Foreground(mywindow.WHITE());
   mywindow.Set_Name("Demo - by Michael Weeks");
   mywindow.Set_Height(MaxYCoord * bsize);
   mywindow.Set_Width(MaxXCoord * bsize);
   mywindow.Open();
                                                                               
   unsigned long Background = mywindow.Get_Background();  // background color
   unsigned long BLACK = mywindow.BLACK();          // min color value
 unsigned long WHITE = mywindow.WHITE();          // max color value
   unsigned long color;
                                                                               
   color = rand_long(WHITE+1);
  // cout << "Press the right mouse button, or q, to exit.\n";
   while ()
   {
      if (mywindow.Check_Event())
      {
         switch (mywindow.Get_Event())
 {
            case (KeyPress):
                  if (mywindow.Get_Key() == 'q')
                     quit=true;
                  break;
                                                                               
            case (ButtonPress):
                  // A button was pressed, so take care of it here.
                  switch (mywindow.Get_Button())
                  {
    case (Button1):
                           color = rand_long(WHITE+1);
                           break;
                     case (Button2):
                     default:
                           quit=true;
                  }
                  break;
                                                                               
            case (MotionNotify):
x = mywindow.Get_Cursor_X();
                  y = mywindow.Get_Cursor_Y();
                  // Draw a figure here
                 // for (int i=0; i<bsize; i++)
                 // {
                   //  for (int j=0; j<bsize; j++)
                    // {
                      //  if (color_array[i+j*bsize]==1)
                        //   mywindow.DrawPoint(x+i, y+j, color);
                       // else
          mywindow.DrawPoint(50, 50, Background);
                     }
                  }
                 // break;
            case (Expose):
                  // redraw the screen here.
                  break;
         }
      }
      // Do other stuff between events.
}
                                                                               
   // Example #2. This is a window showing a text message.
   // Open a second window to confirm quit.
   mywindow2.Open();
   mywindow2.Draw_Text("Are you sure? Press q to quit.");
   quit = false;
   cout << "win2: opening window 2   (Press q in the window to exit)" << endl;
   while (quit==false)
   {
 if (mywindow2.Check_Event())
       {
         if (mywindow2.Get_Event() == KeyPress)
         {
            if (mywindow2.Get_Key() == 'q')
               quit=true;
         }
         // Redraw window if needed.
         else if (mywindow2.Get_Event() == Expose)
            mywindow2.Draw_Text("Are you sure? Press q to quit.");
}
   }
   cout << "win2: closing window 2" << endl;
   mywindow2.Close();
                                                                               
   mywindow.Close();
                                                                               
                                                                               
   // Example #3. This is a window showing a text message.
   // Open a third window, wait, then quit.
 Xwindow mywindow3;
   mywindow3.Set_Background(mywindow3.BLACK());
   mywindow3.Set_Foreground(mywindow3.WHITE());
   mywindow3.Open();
   mywindow3.Draw_Text("Goodbye.");
   if (mywindow3.Check_Event()) {}  // Needed or text will not display
   system("sleep 1");    // wait a second
   mywindow3.Close();
                                                                               
   return 0;
}


0
Comment
Question by:shahrine99
2 Comments
 

Author Comment

by:shahrine99
ID: 18900844
hello?
0
 
LVL 6

Accepted Solution

by:
Sanktwo earned 500 total points
ID: 18920381
Shahrine, which topic did you post this to? I am reading it in software/cygwin which is primarily for help in getting *nix utilities running in MS Windows.

I think it is probably not an appropriate list to get help on X-Window programming.

This may be an example of the new EE posting wizard "helping" you to post to the inappropriate list ;-)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question