Article View: cmu.comp.lang.c++
Article #3C++ WINDOWS QUESTION
From: Daniel Teitelbaum
Date: Sun, 22 Oct 1995 19:36
Date: Sun, 22 Oct 1995 19:36
256 lines
6208 bytes
6208 bytes
I posted this question about 10 days ago and got no answer. Just for
the heck of it, I'll try again. This time, I'm including the code.
I have a function that figures out coordinates and draws and fills
rectangles. The function is called DrawEngineer1. All I pass this
function is the device context which then gets passed to standard
windows calls like FillRect. The program runs
fine, draws the rectangles, and then bombs when it tries to exit this
function. Some sort of array index out of bounds error.
If, instead of making a function call I copy the code of the called
function into the body of the calling function, the program works fine.
Is there a problem with passing the device context?
-----------------------------------------*
#define WIN31
#include <owl.h>
#include<stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include "a:\array1.h"
#include "a:\engin1.h"
#include "a:\product.h"
HDC gDC;
HPEN gPEN;
void tableOfSavings(Engineer *);
const int NUMWORKERS = 20;
const int COSTOFLIVING = 35;
char fname[] = "firmsim.out";
ofstream outstream(fname);
Engineer Workers[NUMWORKERS];
Product p1;
class TMyApp : public TApplication
{
public:
TMyApp (LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
: TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
virtual void InitMainWindow();
};
_CLASSDEF(TMyAppWindow)
class TMyAppWindow : public TWindow
{
public:
COLORREF color;
HPEN ThePen;
int PenSize;
TMyAppWindow(PTWindowsObject AParent, LPSTR ATitle);
~TMyAppWindow();
virtual void WMLButtonDown(RTMessage Msg)
= [WM_FIRST + WM_LBUTTONDOWN];
virtual void DrawEngineer1(HDC PaintDC);
virtual void DrawEngineers2(HDC PaintDC);
virtual void RunSim1(HDC PaintDC);
virtual void tableOfSavings(Engineer *);
};
TMyAppWindow::TMyAppWindow(PTWindowsObject AParent, LPSTR ATitle)
: TWindow(AParent, ATitle)
{
PenSize = 20;
color = 0x00FFFFFF;
gPEN = CreatePen(PS_SOLID, PenSize, color);
}
TMyAppWindow::~TMyAppWindow()
{
DeleteObject(gPEN);
}
void TMyAppWindow::tableOfSavings(Engineer w[])
{
int i;
for (i = 0; i < NUMWORKERS; i++)
outstream << setw(5) << i;
outstream << endl;
for (i = 0; i < NUMWORKERS; i++)
outstream << setw(5) << w[i].getSavings();
outstream << endl;
}
void TMyAppWindow::RunSim1(HDC PaintDC)
{
int death = 0;
int round = 1;
int share = p1.getPrice();
int i;
outstream << p1.getSkills() << endl << "****" << endl;
//for (i = 0; i < NUMWORKERS; i++)
// Workers[i].location = i;
DrawEngineer1(PaintDC); // <-<-<-******************************
// THIS IS WHERE THE BUG OCCURS.
// IT HAPPENS WHEN THE PROGRAM EXITS
// THE DRAWENGINEER1 FUNCTION
// IF I REPLACE THIS CALL WITH THE CODE
// FROM THE DRAWENGINEER1 FUNCTION,
// IT SEEMS TO RUN FINE
// *************************************
while (!death)
{
for (i = 0; i < NUMWORKERS; i++)
{
Workers[i].all_offers.init();
Workers[i].offer_index = 1;
}
//DrawEngineers2(PaintDC);
tableOfSavings(Workers); // everyones savings
for (i = 0; i < NUMWORKERS; i++)
{
if (Workers[i].countCoworkers() > 0)
share = p1.getPrice()/Workers[i].countCoworkers();
Workers[i].decSavings(COSTOFLIVING);
if (Workers[i].getSavings() <= 0)
++death;
if (Workers[i].skillsWeNeed(p1).sum() == 0)
Workers[i].incSavings(share);
}
for (i = 0; i < NUMWORKERS; i++)
{
int offer = (Workers[i]).makeOffer(Workers, p1, NUMWORKERS);
// outstream << "Worker " << i << " makes an offer to worker "
//<< offer << endl;
}
for (i = 0; i < NUMWORKERS; i++)
{
outstream << "Worker " << i << " accepts "
<< Workers[i].considerOffer(Workers, p1) << endl;
}
for (i = 0; i < NUMWORKERS; i++)
{
Workers[i].Join(Workers);
}
++round;
} // while everyone is alive
}
void TMyAppWindow::DrawEngineers2(HDC PaintDC)
{
HBRUSH gbr, cbr;
RECT rc;
RECT *rectptr;
for (int i = 0; i < 4; i++)
{
rc.left = 50+(i*60);
rc.right = 100+(i*60);
rc.top = 150+(i*60);
rc.bottom = 200+(i*60);
*rectptr = rc;
if ( (i % 2) == 0)
{
cbr = CreateSolidBrush(RGB(123, 255, 10));
FillRect(PaintDC, rectptr, cbr);
}
else
{
cbr = CreateSolidBrush(RGB(200, 255, 50));
FillRect(PaintDC, rectptr, cbr);
}
}
TextOut(PaintDC, 83, 83, "Hello", 5);
//******************************************************************
//RunSim1();
}
void TMyAppWindow::DrawEngineer1(HDC PaintDC)
{
HBRUSH gbr, cbr;
RECT rc;
RECT *rectptr;
for (int i = 0; i < 4; i++)
{
rc.left = 50+(i*50);
rc.right = 100+(i*50);
rc.top = 150+(i*50);
rc.bottom = 200+(i*50);
*rectptr = rc;
if ( (i % 2) == 0)
{
cbr = CreateSolidBrush(RGB(0, 255, 255));
FillRect(PaintDC, rectptr, cbr);
}
else
{
cbr = CreateSolidBrush(RGB(200, 0, 50));
FillRect(PaintDC, rectptr, cbr);
}
}
TextOut(PaintDC, 83, 83, "Hello", 5);
//******************************************************************
//RunSim1();
}
void TMyAppWindow::WMLButtonDown(RTMessage)
{
gDC = GetDC(HWindow);
RunSim1(gDC);
ReleaseDC(HWindow, gDC);
}
void TMyApp::InitMainWindow()
{
MainWindow = new TMyAppWindow(NULL, Name);
}
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
TMyApp MyApp("Sample Objects Window Program", hInstance, hPrevInstance,
lpCmdLine, nCmdShow);
MyApp.Run();
return MyApp.Status;
}
Technical Details
Message-ID:
<EkWdp9K00iWPA3tmVz@andrew.cmu.edu>Path:
arc1-ca.pugleaf.net!andrew.cmu.edu!dt2m+Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!andrew.cmu.edu!dt2m+ From: Daniel Teitelbaum <dt2m+@andrew.cmu.edu> Newsgroups: cmu.comp.lang.c++,comp.lang.c++ Subject: C++ WINDOWS QUESTION Date: Sun, 22 Oct 1995 15:36:41 -0400 Organization: Doctoral student, Engineering and Public Policy, Carnegie Mellon, Pittsburgh, PA Message-ID: <EkWdp9K00iWPA3tmVz@andrew.cmu.edu> NNTP-Posting-Host: po7.andrew.cmu.edu