How can I add new ncurses subwindows in response to user demand? - ncurses

I'm trying to write an ncurses program which adds new windows in response to the user pressing keys. For example, consider the following C++ code:
#include <iostream>
#include "curses.h"
using namespace std;
int main()
{
WINDOW * win = initscr();
start_color();
noecho();
WINDOW * sub = subwin(win, 20, 20, 2, 2);
wborder(sub, 0, 0, 0, 0, 0, 0, 0, 0);
keypad(win, TRUE);
while (true)
{
int c = wgetch(win);
if (c == KEY_DOWN)
{
WINDOW* box = subwin(sub, 2, 2, (rand() % 20) + 2, (rand() % 20) + 2);
wborder(box, 0, 0, 0, 0, 0, 0, 0, 0);
}
else if (c == KEY_UP)
{
wrefresh(sub);
}
}
endwin();
return 0;
}
The user can press the down key to create new windows as many times as they want, but wrefresh will only draw them once. This appears to be related to the call to wgetch, a program that doesn't respond to keys works fine. Calling refresh also causes the problem.

The subwin man page says:
When using this routine, it is necessary to call touchwin or
touchline on orig before calling wrefresh on the subwindow.
Creating a sub-window does not actually change the parent window, so after the first refresh the parent window has not changed, touching the window marks it as changed.
So change:
wrefresh(sub);
to
touchwin(sub);
wrefresh(sub);

Related

Error in access different views

I want to create Axial, Sagittal and Coronal views from a volume. At first I say that my volume is achieved from a series of DICOM images using DicomSeriesReadImageWrite2.cxx from itk library. The output is a .mha file.
Now I want to create different views using this data. So I used image ImageSlicing.cxx example. But the out put is a black window.
What shoud I do?
This is the code:
#include <vtkSmartPointer.h>
#include <vtkImageReader2.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include "vtkSmartPointer.h"
#include "vtkImageReader2.h"
#include "vtkMatrix4x4.h"
#include "vtkImageReslice.h"
#include "vtkLookupTable.h"
#include "vtkImageMapToColors.h"
#include "vtkImageActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkCommand.h"
#include "vtkImageData.h"
#include "vtkImageMapper3D.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
// The mouse motion callback, to turn "Slicing" on and off
class vtkImageInteractionCallback : public vtkCommand
{
public:
static vtkImageInteractionCallback *New() {
return new vtkImageInteractionCallback;
};
vtkImageInteractionCallback() {
this->Slicing = 0;
this->ImageReslice = nullptr;
this->Interactor = nullptr;
};
void SetImageReslice(vtkImageReslice *reslice) {
this->ImageReslice = reslice;
};
vtkImageReslice *GetImageReslice() {
return this->ImageReslice;
};
void SetInteractor(vtkRenderWindowInteractor *interactor) {
this->Interactor = interactor;
};
vtkRenderWindowInteractor *GetInteractor() {
return this->Interactor;
};
void Execute(vtkObject *, unsigned long event, void *) override
{
vtkRenderWindowInteractor *interactor = this->GetInteractor();
int lastPos[2];
interactor->GetLastEventPosition(lastPos);
int currPos[2];
interactor->GetEventPosition(currPos);
if (event == vtkCommand::LeftButtonPressEvent)
{
this->Slicing = 1;
}
else if (event == vtkCommand::LeftButtonReleaseEvent)
{
this->Slicing = 0;
}
else if (event == vtkCommand::MouseMoveEvent)
{
if (this->Slicing)
{
vtkImageReslice *reslice = this->ImageReslice;
// Increment slice position by deltaY of mouse
int deltaY = lastPos[1] - currPos[1];
reslice->Update();
double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
// move the center point that we are slicing through
double point[4];
double center[4];
point[0] = 0.0;
point[1] = 0.0;
point[2] = sliceSpacing * deltaY;
point[3] = 1.0;
matrix->MultiplyPoint(point, center);
matrix->SetElement(0, 3, center[0]);
matrix->SetElement(1, 3, center[1]);
matrix->SetElement(2, 3, center[2]);
interactor->Render();
}
else
{
vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
interactor->GetInteractorStyle());
if (style)
{
style->OnMouseMove();
}
}
}
};
private:
// Actions (slicing only, for now)
int Slicing;
// Pointer to vtkImageReslice
vtkImageReslice *ImageReslice;
// Pointer to the interactor
vtkRenderWindowInteractor *Interactor;
};
// The program entry point
int main(int argc, char *argv[])
{
//if (argc < 2)
//{
// cout << "Usage: " << argv[0] << " DATADIR/headsq/quarter" << endl;
// return 1;
//}
// Start by loading some data.
std::string inputFilename = "out.mha";
// Read file
vtkSmartPointer<vtkImageReader2> reader =
vtkSmartPointer<vtkImageReader2>::New();
reader->SetFileName(inputFilename.c_str());
reader->SetDataScalarTypeToUnsignedChar();
reader->Update();
// Calculate the center of the volume
int extent[6];
double spacing[3];
double origin[3];
reader->GetOutputInformation(0)->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), extent);
reader->GetOutput()->GetSpacing(spacing);
reader->GetOutput()->GetOrigin(origin);
double center[3];
center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
// Matrices for axial, coronal, sagittal, oblique view orientations
//static double axialElements[16] = {
// 1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 0, 0, 0, 1 };
//static double coronalElements[16] = {
// 1, 0, 0, 0,
// 0, 0, 1, 0,
// 0,-1, 0, 0,
// 0, 0, 0, 1 };
static double sagittalElements[16] = {
0, 0,-1, 0,
1, 0, 0, 0,
0,-1, 0, 0,
0, 0, 0, 1 };
//static double obliqueElements[16] = {
// 1, 0, 0, 0,
// 0, 0.866025, -0.5, 0,
// 0, 0.5, 0.866025, 0,
// 0, 0, 0, 1 };
// Set the slice orientation
vtkSmartPointer<vtkMatrix4x4> resliceAxes =
vtkSmartPointer<vtkMatrix4x4>::New();
resliceAxes->DeepCopy(sagittalElements);
// Set the point through which to slice
resliceAxes->SetElement(0, 3, center[0]);
resliceAxes->SetElement(1, 3, center[1]);
resliceAxes->SetElement(2, 3, center[2]);
// Extract a slice in the desired orientation
vtkSmartPointer<vtkImageReslice> reslice =
vtkSmartPointer<vtkImageReslice>::New();
reslice->SetInputConnection(reader->GetOutputPort());
reslice->SetOutputDimensionality(2);
reslice->SetResliceAxes(resliceAxes);
reslice->SetInterpolationModeToLinear();
// Create a greyscale lookup table
vtkSmartPointer<vtkLookupTable> table =
vtkSmartPointer<vtkLookupTable>::New();
table->SetRange(0, 2000); // image intensity range
table->SetValueRange(0.0, 1.0); // from black to white
table->SetSaturationRange(0.0, 0.0); // no color saturation
table->SetRampToLinear();
table->Build();
// Map the image through the lookup table
vtkSmartPointer<vtkImageMapToColors> color =
vtkSmartPointer<vtkImageMapToColors>::New();
color->SetLookupTable(table);
color->SetInputConnection(reslice->GetOutputPort());
// Display the image
vtkSmartPointer<vtkImageActor> actor =
vtkSmartPointer<vtkImageActor>::New();
actor->GetMapper()->SetInputConnection(color->GetOutputPort());
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
vtkSmartPointer<vtkRenderWindow> window =
vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer(renderer);
// Set up the interaction
vtkSmartPointer<vtkInteractorStyleImage> imageStyle =
vtkSmartPointer<vtkInteractorStyleImage>::New();
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetInteractorStyle(imageStyle);
window->SetInteractor(interactor);
window->Render();
vtkSmartPointer<vtkImageInteractionCallback> callback =
vtkSmartPointer<vtkImageInteractionCallback>::New();
callback->SetImageReslice(reslice);
callback->SetInteractor(interactor);
imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);
// Start interaction
// The Start() method doesn't return until the window is closed by the user
interactor->Start();
return EXIT_SUCCESS;
}
and this is the output:
The problem was in reading data. I used pointer to read my data and problem solved.

How to absolutely ensure Direct3D9 hook overlay rendering on top

I'm trying to hook IDirect3DDevice9::Present or ::EndScene and render my own overlay (which, for now, is just a simple rectangle) on top of everything else in a D3D9 application, but my overlay seems to be appearing and disappearing quite randomly. The drawing code I'm currently using is:
typedef struct CUSTOMVERTEX {
float x, y, z, rwh;
DWORD color;
};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
void draw() {
// the positions are just for testing purposes, so they don't really make sense
CUSTOMVERTEX vertices[] = {
{ 0, 0, 0, 1.0f, D3DCOLOR_XRGB(255, 255, 255) },
{ 0, cursor_pos.y+500, 0, 1.0f, D3DCOLOR_XRGB(127, 255, 255) },
{ cursor_pos.x, cursor_pos.y, 0, 1.0f, D3DCOLOR_XRGB(255,255, 255) },
{ cursor_pos.x, 600, 0, 1.0f, D3DCOLOR_XRGB(127, 0, 0) }
};
if (vBuffer == 0) return;
VOID* pVoid;
vBuffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
vBuffer->Unlock();
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
D3DMATRIX orthographicMatrix;
D3DMATRIX identityMatrix;
// MAKE_D3DMATRIX should be equivalent to D3DXMATRIX constructor
D3DMATRIX viewMatrix = MAKE_D3DMATRIX(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
((float)-(get_window_width()/2)), ((float)-(get_window_height()/ 2)), 0, 1
);
// MAKE_ORTHO_LH is equivalent to D3DMatrixOrthoLH
MAKE_ORTHO_LH(&orthographicMatrix, (FLOAT)get_window_width(), (FLOAT)get_window_height(), -1.0, 1.0);
// and this to D3DMatrixIdentity
MAKE_IDENTITY(&identityMatrix); // and this to D3DMatrixIdentity
d3dDevice->SetTransform(D3DTS_PROJECTION, &orthographicMatrix);
d3dDevice->SetTransform(D3DTS_WORLD, &identityMatrix);
d3dDevice->SetTransform(D3DTS_VIEW, &viewMatrix);
d3dDevice->SetRenderState(D3DRS_ZENABLE, false);
d3dDevice->SetFVF(CUSTOMFVF);
d3dDevice->SetStreamSource(0, vBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
d3dDevice->SetRenderState(D3DRS_ZENABLE, true);
}
I can't seem to figure out why this would cause the overlay to pop in and out of existence at seemingly random points in time.. What am I missing?
I found a foolproof method to render my stuff directly to the backbuffer (on CPU):
IDirect3DSwapChain9 *sc;
if (FAILED(d3dDevice->GetSwapChain(0, &sc))) {
PRINT("GetSwapChain failed\n");
return;
}
IDirect3DSurface9 *s;
if (FAILED(sc->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &s))) {
PRINT("GetBackBuffer failed\n");
return;
}
D3DLOCKED_RECT r;
if (FAILED(s->LockRect(&r, NULL, D3DLOCK_DONOTWAIT))) {
PRINT("LockRect failed\n");
return;
}
// here, find out pixel format and manipulate
// pixel buffer through r->pBits, using r->Pitch accordingly
s->UnlockRect();
s->Release();
sc->Release();
Will most probably incur a performance penalty, but in my application this doesn't really make a difference. A more optimal way would be to harness the GPU's hwacceled rendering capabilities, but I currently lack the D3D knowledge to do so.

Does refresh() need to be called at least once, when using windows in ncurses?

I've been testing ncurses, I tried doing a simple code using windows, by reading the code from the tutorials it seemed to me like calling wrefresh() was enough if changes were made just to one window. So I tried the following code, but it doesn't work, does anyone know why?
#include <ncurses.h>
int main (void) {
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
WINDOW *my_window = newwin(10, 20, 3, 4);
box(my_window, 0, 0);
wrefresh(my_window);
while ((ch=getch()) != 'q');
endwin();
return 0;
}
If I add an extra call to refresh() before wrefresh() everything works fine.
#include <ncurses.h>
int main (void) {
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
WINDOW *my_window = newwin(10, 20, 3, 4);
box(my_window, 0, 0);
refresh();
wrefresh(my_window);
while ((ch=getch()) != 'q');
endwin();
return 0;
}
I've tried several things, for instance calling refresh() after wrefresh() doesn't work either, using only refresh() also does not work. Also example 7 in this guide shows after calling refresh() once, it is enough to just call wrefresh() in the while loop.
Is it always mandatory to make a call to refresh() at least once after initscr()? the documentation does not seem to mention this.
Calling getch is the same as wgetch(stdscr). When you call wgetch, it refreshes the window that it uses as a parameter. If you are trying to refresh a different window (such as my_window), then you should use that window as the parameter.
In your example, there is nothing interesting that is written to stdscr. So you can omit the plain refresh() call.
With those changes, the program is
#include <ncurses.h>
int main (void) {
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
WINDOW *my_window = newwin(10, 20, 3, 4);
box(my_window, 0, 0);
while ((ch=wgetch(my_window)) != 'q');
endwin();
return 0;
}
For a more interesting demo, you could write into the window. That is best done with a subwindow, so that it can be scrolled, e.g.,
#include <ncurses.h>
int main (void) {
WINDOW *my_window;
WINDOW *my_scroller;
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
if ((my_window = newwin(10, 20, 3, 4)) != 0) {
box(my_window, 0, 0);
wrefresh(my_window);
if ((my_scroller = derwin(my_window, 8, 18, 1, 1)) != 0) {
scrollok(my_scroller, TRUE);
while ((ch=wgetch(my_scroller)) != 'q') {
wprintw(my_scroller, "%#x - %s\n", ch, keyname(ch));
}
}
}
endwin();
return 0;
}

Trouble with drawing a pixel through Draw_Pixel() of SDL_Draw library

It might sound a little strange, but I can't correctly use the Draw_Pixel() and Draw_Line() functions of SDL_Draw.h
I've googled and people state: It's very simple to use, but unfortunately... not for me.
I've written a very simple piece of code but it doesn't show me the pixel, I tried also with a line, but nothing as well.
The simple code is:
#include <SDL.h>
#include "SDL_draw.h"
int main(int argc, char** argv)
{
SDL_Surface* surf;
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return -1;
}
if((surf = SDL_SetVideoMode(WIDTH, HEIGTH, BBP, FLAGS)) == NULL) {
return -1;
}
Uint32 myColor = SDL_MapRGB( surf->format, 255, 25, 23); //this is the red color
Draw_Pixel(surf, 45, 45, myColor);
Draw_Line(surf, 45, 20, 90, 100, myColor);
SDL_Quit();
return 1;
}
The x,y coordinates are just random. I'm trying to see the result with breakpoints in Visual Studio.
Can anyone tell me where I'm mistaking?
You have to update your surface by calling SDL_Flip( SDL_Surface * screen) ;
And add a delay before exiting the program, otherwise you wont see the window.

Allegro program triggers breakpoint?

So I'm learning about sprite programming and we're using allegro. When I run one of the sample programs I get the message: rotatesprite.exe has triggered a breakpoint. This was in visual studio. I can't get allegro to work outside of visual studio
sample program:
#include <allegro.h>
#define WHITE makecol(255,255,255)
int main(void)
{
int x, y;
float angle = 0;
BITMAP *tank;
//initialize program
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
textout_ex(screen,font,"Rotate: LEFT / RIGHT arrow keys",
0,0,WHITE,0);
//load tank sprite
tank = load_bitmap("C:\Users\Jason\Desktop\module7\tank.bmp", NULL);
//calculate center of screen
//x = SCREEN_W/2 - tank->w/2;
//y = SCREEN_H/2 - tank->h/2;
x=SCREEN_W/2;
y=SCREEN_H/2;
//draw tank at starting location
rotate_sprite(screen, tank, x, y, 0);
//main loop
while(!key[KEY_ESC])
{
//wait for keypress
if (keypressed())
{
//left arrow rotates left
if (key[KEY_LEFT])
{
angle -= 0.1;
if (angle < 0) angle = 256;
rotate_sprite(screen, tank, x, y, itofix(angle));
}
//right arrow rotates right
if (key[KEY_RIGHT])
{
angle += 0.1;
if (angle > 256) angle = 0;
rotate_sprite(screen, tank, x, y, itofix(angle));
}
//display angle
textprintf_ex(screen, font, 0, 10, WHITE, 0,
"Angle = %f", angle);
}
}
allegro_exit();
return 0;
}
END_OF_MAIN()
the program triggering the breakpoint is: crt0msg.c off of the disk.
snippet of code:
#ifdef _DEBUG
/*
* Report error.
*
* If _CRT_ERROR has _CRTDBG_REPORT_WNDW on, and user chooses
* "Retry", call the debugger.
*
* Otherwise, continue execution.
*
*/
if (rterrnum!=_RT_CRNL&&rterrnum!=_RT_BANNER&&rterrnum!=_RT_CRT_NOTINIT)
{
if (1 == _CrtDbgReport(_CRT_ERROR, NULL, 0, NULL,rterrs[tblindx].rterrtxt))
_CrtDbgBreak();
}
#endif /* _DEBUG */
tank = load_bitmap("C:\Users\Jason\Desktop\module7\tank.bmp", NULL);
Your compiler should be warning you about that string since it contains invalid escaped characters. You should use double back slashes or single forward slashes:
tank = load_bitmap("C:\\Users\\Jason\\Desktop\\module7\\tank.bmp", NULL);
// or
tank = load_bitmap("C:/Users/Jason/Desktop/module7/tank.bmp", NULL);
The latter format is recommended because it is cross platform. (Minus the whole bit about hard coding an absolute path.)
Finally, you really need to check return codes:
if (!tank) {
// gracefully report error and exit
}
Otherwise the program will crash somewhere else and it will be harder to debug.

Resources