Solved

c++/cygwin

Posted on 2007-04-11
2
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

735 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