home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d186 / cards'o'rama.lha / Cards'O'Rama / Sources / sources.zoo / game.c < prev    next >
C/C++ Source or Header  |  1989-02-25  |  9KB  |  348 lines

  1. /* ******************************************************************** */
  2. /*                                game.c                                */
  3. /*                                                                      */
  4. /* December 4, 1988: After a few months spent over C exercises and very */
  5. /* simple programs based on various examples, this is my FIRST PROGRAM  */
  6. /* and though it's apparently a cards game, actually it's just another  */
  7. /* exercise that deals with screens, windows, menus and requesters...   */
  8. /*                                                                      */
  9. /* HISTORY              DESCRIPTION                                     */
  10. /* -------              -----------                                     */
  11. /* 29-Jan-89            clean-up for release                            */
  12. /* 25-Jan-89            fixed minor bugs                                */
  13. /* 21-Jan-89            amigamoves.c completed                          */
  14. /* 12-Jan-89            game ready (in two players mode)                */
  15. /* 09-Dec-89            drawn all cards                                 */
  16. /* 04-Dec-89            started game programming                        */
  17. /*                                                                      */
  18. /* ******************************************************************** */
  19.  
  20.  
  21.  
  22. #include <intuition/intuition.h>
  23. #include <graphics/gfxmacros.h>
  24. #include <exec/types.h>
  25. #include <exec/memory.h>
  26. #include <stdlib.h>
  27.  
  28. #include "mydefines.h"
  29. #include "myscreen.h"
  30. #include "mywindow.h"
  31. #include "menustuff.h"
  32. #include "event.c"
  33. #include "menupick.c"
  34. #include "shufflecards.c"
  35. #include "displaycards.c"
  36. #include "intascii.c"
  37. #include "displaydata.c"
  38. #include "gameloop.c"
  39. #include "update.c"
  40. #include "deletecard.c"
  41. #include "writestring.c"
  42. #include "initrequesters.c"
  43. #include "amigamoves.c"
  44.  
  45. struct GfxBase          *GfxBase;
  46. struct IntuitionBase    *IntuitionBase;
  47.  
  48. struct Screen           *OpenScreen();
  49. struct Screen           *s;
  50. struct ViewPort         *vp;
  51. struct Window           *OpenWindow();
  52. struct Window           *w;
  53. struct RastPort         *rp;
  54. struct Remember         *key;
  55.  
  56. UWORD mycolortable[] =
  57.    {
  58.    0x0000, 0x0eca, 0x0006, 0x0aaa, 0x0fb5, 0x0fd7, 0x0090, 0x0070,
  59.    0x01a4, 0x0aa0, 0x02cf, 0x069c, 0x0f66, 0x0f88, 0x0fbb, 0x0c9f,
  60.    0x0850, 0x0e52, 0x0a52, 0x0fca, 0x0f44, 0x0f88, 0x0fa2, 0x0fd4,
  61.    0x0888, 0x0999, 0x0aa9, 0x0bba, 0x0eed, 0x0fff, 0x069d, 0x0a00
  62.    };
  63.  
  64. UWORD mycardsposition[] = {0,          /* This one is not really used! */
  65.       1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
  66.       10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16};
  67.  
  68. int showtime = 30;                     /* How long to diplay the deck */
  69. int peektime = 12;                     /* How long you can peek */
  70. int shuffles = 40;                     /* How many times to shuffle */
  71.  
  72. int memory = 2;                        /* How many moves to remember */
  73. int percentage = 10;                   /* % of peeking by computer */
  74. int enemy = TRUE;                      /* My enemy (TRUE: Amiga) */
  75.  
  76. int turn1 = 0;
  77. int turn2 = 0;
  78. int score1 = 0;
  79. int score2 = 0;
  80. int peek1 = 0;
  81. int peek2 = 0;
  82.  
  83. int selectbutton;
  84. int currentmousex;
  85. int currentmousey;
  86.  
  87. struct IntuiMessage *mess;
  88.  
  89. _main()
  90.    {
  91.    extern USHORT mycards[];            /* image data */
  92.    extern struct Image mycardsimage[]; /* Image structures */
  93.  
  94.    extern USHORT naahdataoff[];
  95.    extern struct Image naahimageoff;
  96.    extern USHORT naahdataon[];
  97.    extern struct Image naahimageon;
  98.    extern struct Gadget naahgadget;
  99.  
  100.    extern USHORT yeahdataoff[];
  101.    extern struct Image yeahimageoff;
  102.    extern USHORT yeahdataon[];
  103.    extern struct Image yeahimageon;
  104.    extern struct Gadget yeahgadget;
  105.  
  106.    extern USHORT clickdataoff[];
  107.    extern struct Image clickimageoff;
  108.    extern USHORT clickdataon[];
  109.    extern struct Image clickimageon;
  110.    extern struct Gadget clickgadget;
  111.  
  112.    UWORD *ptrold;                      /* Pointer to CHIP MEMORY */
  113.    UWORD *ptrnew;                      /* Working pointer to CHIP MEMORY */
  114.  
  115.    int i;
  116.    int retvalue = DUMMY_VALUE;
  117.  
  118.  
  119.    key = NULL;
  120.  
  121.    if(!(GfxBase = (struct GfxBase *)
  122.    OpenLibrary("graphics.library", 0L)))
  123.       {
  124.       Write(Output(), "\nERROR: Unable to open graphics.library!\n\n", 42);
  125.       cleanup();
  126.       Exit(10);
  127.       }
  128.  
  129.    if(!(IntuitionBase = (struct IntuitionBase *)
  130.    OpenLibrary("intuition.library", 0L)))
  131.       {
  132.       Write(Output(), "\nERROR: Unable to open intuition.library!\n\n", 43);
  133.       cleanup();
  134.       Exit(20);
  135.       }
  136.  
  137.    if(!(s = OpenScreen(&myscreen)))
  138.       {
  139.       Write(Output(), "\nERROR: Unable to open screen <myscreen>!\n\n", 43);
  140.       cleanup();
  141.       Exit(30);
  142.       }
  143.    mywindow.Screen = s;
  144.    vp = &(s -> ViewPort);
  145.    LoadRGB4(vp, &mycolortable[0], 32);
  146.  
  147.    if(!(w = OpenWindow(&mywindow)))
  148.       {
  149.       Write(Output(), "\nERROR: Unable to open window <mywindow>!\n\n", 43);
  150.       cleanup();
  151.       Exit(40);
  152.       }
  153.    rp = w -> RPort;
  154.  
  155.  
  156.  
  157. /* Allocate enough (CHIP) memory for image data */
  158.  
  159.    if(!(ptrold = (UWORD *)AllocRemember(&key, ARRAYSIZE * 2, MEMF_CHIP)))
  160.       {
  161.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  162.       cleanup();
  163.       Exit(50);
  164.       }
  165.    ptrnew = ptrold;
  166.  
  167. /* Now I want to move the image data into CHIP MEMORY */
  168.  
  169.    for (i = 0; i < ARRAYSIZE; i++)
  170.       *ptrnew++ = mycards[i];
  171.    ptrnew = ptrold;
  172.  
  173. /* Initialize all the Image structures */
  174.  
  175.    for (i = 0; i < CARDNUM; i++)
  176.       mycardsimage[i].ImageData = ptrnew + CARDSIZE * i;
  177.  
  178.  
  179.  
  180. /* Allocate enough (CHIP) memory for image data (gadgets) */
  181.  
  182.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  183.       {
  184.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  185.       cleanup();
  186.       Exit(60);
  187.       }
  188.    ptrnew = ptrold;
  189.  
  190. /* Now I want to move the image data into CHIP MEMORY */
  191.  
  192.    for (i = 0; i < PEEKIMAGESIZE; i++)
  193.       *ptrnew++ = naahdataoff[i];
  194.    ptrnew = ptrold;
  195.  
  196. /* Initialize the Image structures */
  197.  
  198.       naahimageoff.ImageData = ptrnew;
  199.  
  200. /* Allocate enough (CHIP) memory for image data (gadgets) */
  201.  
  202.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  203.       {
  204.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  205.       cleanup();
  206.       Exit(70);
  207.       }
  208.    ptrnew = ptrold;
  209.  
  210. /* Now I want to move the image data into CHIP MEMORY */
  211.  
  212.    for (i = 0; i < PEEKIMAGESIZE; i++)
  213.       *ptrnew++ = naahdataon[i];
  214.    ptrnew = ptrold;
  215.  
  216. /* Initialize the Image structures */
  217.  
  218.       naahimageon.ImageData = ptrnew;
  219.  
  220. /* Allocate enough (CHIP) memory for image data (gadgets) */
  221.  
  222.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  223.       {
  224.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  225.       cleanup();
  226.       Exit(80);
  227.       }
  228.    ptrnew = ptrold;
  229.  
  230. /* Now I want to move the image data into CHIP MEMORY */
  231.  
  232.    for (i = 0; i < PEEKIMAGESIZE; i++)
  233.       *ptrnew++ = yeahdataoff[i];
  234.    ptrnew = ptrold;
  235.  
  236. /* Initialize the Image structures */
  237.  
  238.       yeahimageoff.ImageData = ptrnew;
  239.  
  240. /* Allocate enough (CHIP) memory for image data (gadgets) */
  241.  
  242.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  243.       {
  244.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  245.       cleanup();
  246.       Exit(90);
  247.       }
  248.    ptrnew = ptrold;
  249.  
  250. /* Now I want to move the image data into CHIP MEMORY */
  251.  
  252.    for (i = 0; i < PEEKIMAGESIZE; i++)
  253.       *ptrnew++ = yeahdataon[i];
  254.    ptrnew = ptrold;
  255.  
  256. /* Initialize the Image structures */
  257.  
  258.       yeahimageon.ImageData = ptrnew;
  259.  
  260. /* Allocate enough (CHIP) memory for image data (gadgets) */
  261.  
  262.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  263.       {
  264.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  265.       cleanup();
  266.       Exit(100);
  267.       }
  268.    ptrnew = ptrold;
  269.  
  270. /* Now I want to move the image data into CHIP MEMORY */
  271.  
  272.    for (i = 0; i < PEEKIMAGESIZE; i++)
  273.       *ptrnew++ = clickdataoff[i];
  274.    ptrnew = ptrold;
  275.  
  276. /* Initialize the Image structures */
  277.  
  278.       clickimageoff.ImageData = ptrnew;
  279.  
  280. /* Allocate enough (CHIP) memory for image data (gadgets) */
  281.  
  282.    if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
  283.       {
  284.       Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
  285.       cleanup();
  286.       Exit(110);
  287.       }
  288.    ptrnew = ptrold;
  289.  
  290. /* Now I want to move the image data into CHIP MEMORY */
  291.  
  292.    for (i = 0; i < PEEKIMAGESIZE; i++)
  293.       *ptrnew++ = clickdataon[i];
  294.    ptrnew = ptrold;
  295.  
  296. /* Initialize the Image structures */
  297.  
  298.       clickimageon.ImageData = ptrnew;
  299.  
  300.  
  301.  
  302. /* And finally we drop into the main loop!!! */
  303.  
  304.    initrequesters();
  305.    SetRast(rp, 6);
  306.    SetMenuStrip(w, &mygamemenu[0]);
  307.  
  308.    while(1)
  309.       {
  310.       retvalue = waitformess();
  311.       if (retvalue == PICKED_START)
  312.          gameloop();
  313.       }
  314.    return(0);
  315.    }
  316.  
  317.  
  318.  
  319. cleanup()
  320.    {
  321.    ClearMenuStrip(w);
  322.    if (key)
  323.       FreeRemember(&key, TRUE);
  324.    if (w)
  325.       CloseWindow(w);
  326.    if (s)
  327.       CloseScreen(s);
  328.    if (IntuitionBase)
  329.       CloseLibrary(IntuitionBase);
  330.    if (GfxBase)
  331.       CloseLibrary(GfxBase);
  332.    return(0);
  333.    }
  334.  
  335.  
  336.  
  337. waitformess()
  338.    {
  339.    int value;
  340.  
  341.    mess = (struct IntuiMessage *)GetMsg(w -> UserPort);
  342.    if (mess == NULL)
  343.       Wait(1 << w -> UserPort -> mp_SigBit);
  344.    else
  345.       value = handleevent(mess);
  346.    return(value);
  347.    }
  348.