How to show NCursesPanel on screen by c++ bindings of NCurses - c++

DemoI am trying out the c++ bindings shipped with ncurses-5.9. It seems that it is not easy to show a panel with window inside on screen easily. Here is my code
class SpiderApplication : NCursesApplication
{
protected:
int titlesize() const { return 2; };
void title();
public:
SpiderApplication() : NCursesApplication(TRUE) {}
int run();
};
void SpiderApplication::title()
{
const char * const titleText = "Demo";
const int len = ::strlen(titleText);
titleWindow->bkgd(screen_titles());
titleWindow->addstr(0, (titleWindow->cols() - len) / 2, titleText);
titleWindow->noutrefresh();
}
int SpiderApplication::run()
{
NCursesPanel mystd;
NCursesPanel P(mystd.lines() - titlesize(), mystd.cols(), titlesize() - 1, 0);
P.label("Demo", NULL);
P.show();
::getch();
P.clear();
return 0;
}
I suppose that the there should be a panel shown under titleline of this application. However, it doesn't. Do I miss something important here. Is there any example for how to use the c++ binding of ncurses?
Thanks.

I find the answer for my own question.
To make panel shown on screen, refresh() call cannot be omitted. Here is the working code:
NCursesPanel mystd;
NCursesPanel P(mystd.lines() - titlesize(), mystd.cols(), titlesize() - 1, 0);
P.label("Demo", NULL);
P.show();
mystd.refresh();
::getch();
P.clear();

Related

VS2017: cannot add strings/wstrings

I have trouble understanding why the following code does not do what it should, VS2017 does not show an error and the solution is created, but the string is never what it should be:
void COrion::AddJournalMessage(CTextData *msg, const string &name)
{
WISPFUN_DEBUG("c194_f101");
CTextData *jmsg = new CTextData(msg);
jmsg->Text = name + jmsg->Text;
}
jmsg->Text is std::string.
now at runtime let's say 'name' is "Player:" and 'jmsg->Text' is "Hello World", I would expect the text after the code to be "Player:Hello World", but it is not. It's only "Player:" and I don't understand why.
I found a workaround in a way:
jmsg->Text = name.c_str() + jmsg->Text;
with this change it is "Player:Hello World".
Problem is, I still don't understand why the first one does not work.
Can someone explain where the problem is?
Is it specific to VS or something?
to make it clear: this is from an open source project I want to use, not code I wrote myself, but the problem has been the source of many bugs, since it is used in this way alot.
edit
CTextData class:
class CTextData : public CRenderTextObject
{
public:
bool Unicode = false;
TEXT_TYPE Type = TT_CLIENT;
uchar Font = 0;
uint Timer = 0;
uint MoveTimer = 0;
string Text = "";
wstring UnicodeText = L"";
uchar Alpha = 0xFF;
CRenderWorldObject *Owner = NULL;
CTextData();
CTextData(CTextData *obj);
virtual ~CTextData();
virtual bool IsText() { return true; }
bool CanBeDrawedInJournalGump();
CGLTextTexture m_Texture;
void GenerateTexture(
int maxWidth,
ushort flags = 0,
TEXT_ALIGN_TYPE align = TS_LEFT,
uchar cell = 30,
int font = -1);
};

Unreal engine c++ write delegate OnScreenshotCaptured

I am new to C++. I've wrote code in C# and PHP.Since I am using Unreal engine I am trying to learn C++. For my project I need to make a screenshot in-game and show it immediately so I want to get it as a texture.
I made a blueprint node which calls this function i've made:
void UMyBlueprintFunctionLibrary::TakeScreenshot()
{
FScreenshotRequest::RequestScreenshot(true);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Tried to take screenshot");
}
When I hover my mouse above RequestScreenshot I see the following pop-up:
"Screenshot can be read from memory by subscribing to the viewsport OnScreenshopCaptured delegate"
So that is what I try to do but I have no idea how I looked up this:
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameViewportClient/OnScreenshotCaptured/
Can someone tell me how to implement this and how you see/know how to implement it?
I have an alternative, no delegate, but FRenderTarget::ReadPixel() to some buffer you allocated, by implementing your own UGameViewportClient (inherit it), and overriding Draw() function.
I'll show the essential codes, but not complete.
void UMyGameViewportClient::Draw(FViewport* Viewport, FCanvas* SceneCanvas)
{
Super::Draw(Viewport, SceneCanvas);
if (any_condition_you_need) {
CaptureFrame();
}
}
void UMyGameViewportClient::CaptureFrame()
{
if (!Viewport) {
return;
}
if (ViewportSize.X == 0 || ViewportSize.Y == 0) {
return;
}
ColorBuffer.Empty(); // Declare this in header as TArray<FColor>
if (!Viewport->ReadPixels(ColorBuffer, FReadSurfaceDataFlags(),
FIntRect(0, 0, ViewportSize.X, ViewportSize.Y)))
{
return;
}
SaveThumbnailImage();
}
void UMyGameViewportClient::SaveThumbnailImage()
{
IImageWrapperModule& wrappermodule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
auto wrapper_ptr = wrappermodule.CreateImageWrapper(EImageFormat::PNG);
for (int i = 0; i < ColorBuffer.Num(); i++)
{
auto ptr = &ColorBuffer[i];
auto r = ptr->R;
auto b = ptr->B;
ptr->R = b;
ptr->B = r;
ptr->A = 255;
} // not necessary, if you like bgra, just change the following function argument to ERGBFormat::BGRA
wrapper_ptr->SetRaw(&ColorBuffer[0], ColorBuffer.Num() * 4,
ViewportSize.X, ViewportSize.Y, ERGBFormat::RGBA, 8);
FFileHelper::SaveArrayToFile(wrapper_ptr->GetCompressed(), *ThumbnailFile);
}

Compile time error: undefined reference to 'CTetrisGame::Create()'

I'm fairly new to programming in c++ and I've come across some code which gives me errors because the code was intended for a windows operating system, however I'm running a linux operating system. There's a file called TetrisBlock.h, which looks like:
#ifndef TETRIS_BLOCK_INCLUDED
#define TETRIS_BLOCK_INCLUDED
#include "Common.h"
class CTetrisBlock
{
public:
void Create();
void Draw();
void Destroy();
CTetrisBlock();
virtual ~CTetrisBlock();
int GetPosX();
int GetPosY();
void SetPosX(int x);
void SetPosY(int y);
private:
int m_iPosX, m_iPosY;
};
#endif
from my experience with java, this looks like an interface to me, waiting for a .cpp to implement it and fill in the abstract methods. The corresponding TetrisBlock.cpp file is as follows:
#include "TetrisBlock.h"
CTetrisBlock::CTetrisBlock()
{
int num_blocks_x = WINDOW_WIDTH / (BLOCK_SIZE + BLOCK_SPACING);
int num_blocks_y = WINDOW_HEIGHT / (BLOCK_SIZE + BLOCK_SPACING);
m_iPosX = num_blocks_x / 2;
m_iPosY = num_blocks_y - 1;
}
CTetrisBlock::~CTetrisBlock()
{
Destroy();
}
void CTetrisBlock::Create()
{
}
void CTetrisBlock::Draw()
{
tRect quad;
quad.m_iLeft = m_iPosX * (BLOCK_SIZE + BLOCK_SPACING) + BLOCK_SPACING;
quad.m_iRight = quad.m_iLeft + BLOCK_SIZE - BLOCK_SPACING;
quad.m_iTop = m_iPosY * (BLOCK_SIZE + BLOCK_SPACING) - BLOCK_SPACING;
quad.m_iBottom = quad.m_iTop - BLOCK_SIZE + BLOCK_SPACING;
glColor3d(1,1,1);
glBegin(GL_QUADS);
glVertex3f(quad.m_iLeft, quad.m_iBottom, 0);
glVertex3f(quad.m_iRight, quad.m_iBottom, 0);
glVertex3f(quad.m_iRight, quad.m_iTop, 0);
glVertex3f(quad.m_iLeft, quad.m_iTop, 0);
glEnd();
}
void CTetrisBlock::Destroy()
{
}
void CTetrisBlock::SetPosX(int x)
{
m_iPosX = x;
}
void CTetrisBlock::SetPosY(int y)
{
m_iPosY = y;
}
int CTetrisBlock::GetPosX()
{
return m_iPosX;
}
int CTetrisBlock::GetPosY()
{
return m_iPosY;
}
Note: The OpenGL library has already been imported within the common.h file
When I compile the file TetrisBlock.cpp, I get the error lines in the console:
undefined reference to 'CTetrisBlock::Create()'
undefined reference to 'CTetrisBlock::Draw()'
undefined reference to 'CTetrisBlock::Destroy()'
etc...
I'm guessing that the way I've filled in the abstract methods only works on windows, and that there's a different way to do it in linux. If this is true, then how would I fill in those abstract methods on linux. If I'm completely wrong, then can anyone explain why I'm receiving these errors. Also, are .h files always used the same way interfaces are used in java, or do they have a more general use?

Callback function confuses argument?

I have a sfml window container, and it appears to be working, however the glViewPorts are the wrong size, which I assume is because the wrong sf::Window is being passed.
Here is a function which adds to the window: It takes some information about the sfml window.
int WindowContainer::PushBack(WindowData& data)
{
if(data.WindowSettingsOK() && data.VideoModeOK()){
mWindowVector.resize(mWindowVector.size() + 1);
mDisplayFuncVector.resize(mWindowVector.size());
mInputFuncVector.resize(mWindowVector.size());
mWindowVector.at(mWindowVector.size() - 1) = new sf::Window();
mWindowVector.at(mWindowVector.size() - 1)->Create(data.VideoMode(), data.Title(), data.Style(), data.Settings());
mWindowVector.at(mWindowVector.size() - 1)->SetPosition(data.PositionX(), data.PositionY());
mDisplayFuncVector.at(mWindowVector.size() - 1) = nullptr;
mInputFuncVector.at(mWindowVector.size() - 1) = nullptr;
return 0;
}
else{
PrintError(ErrorMessageType::BadSettings);
return 1;
}
}
Alternatively, this function may be called to setup the display and input function callbacks:
int WindowContainer::PushBack(WindowData& data, function_p displayFunc, function_p inputFunc)
{
int return_val = PushBack(data);
mDisplayFuncVector.at(mWindowVector.size() - 1) = displayFunc;
mInputFuncVector.at(mWindowVector.size() - 1) = inputFunc;
return return_val;
}
Then, when the window needs .Display()'ing, this function is called:
void WindowContainer::ProcessDisplay()
{
for(unsigned int i = 0; i < mWindowVector.size(); i ++){
if(mDisplayFuncVector.at(i) != nullptr){
mDisplayFuncVector.at(i)(*mWindowVector.at(i), mClock, (const void*&)mExternalDrawingDataPointer);
}
mWindowVector.at(i)->Display();
}
}
... This is all good, until the result on the screen is that resizing one window affects the viewport of both windows. This suggests that calling the callback function: mDisplayFuncVector.at(i)(*mWindowVector.at(i), mClock, (const void*&)mExternalDrawingDataPointer); gives the argument of *mWindowVector.at(0) each time, instead of each window individually. (As in *mWindowVector.at(i))
Can anyone help with this problem?
The main loop contains this code:
while(container.Access(0)->IsOpened()){
container.ProcessInput();
container.ProcessDisplay();
}
Container.Access(int) is this function:
const sf::Window*& WindowContainer::Access(unsigned int index)
{
if(index > mWindowVector.size()){
PrintError(ErrorMessageType::IndexOutOfRange);
}
else{
return (const sf::Window*&)mWindowVector.at(index);
}
return (const sf::Window*&)mWindowVector.at(0);
}
Thanks again, I'm sure I have made a mistake somewhere but cannot spot it.
I have been thinking about this question and suspect openGL becomes confused with which window is it drawing to if more than one object is pushed back without a call to Display() to sync everything.
I am yet to test this and confirm.
EDIT The window container now works. It has nothing to do with the callback functions argument.

No matching function error in Xcode 3

I am trying to compile a custom class in Xcode 3 and I keep getting a no matching function call error although the same custom class compiles fine under Windows. Obviously something is not right regarding the use of curly brackets and the XCode compiler. The compiler is choking at the first curly bracket { below:
: ADataBrowser(inOwnerWindow,inID,inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
Any ideas much appreciated!
//Full source below
#pragma once
#include <ADataBrowser.h>
#include <AControls.h>
enum
{
kMaxLevelCount = 16
};
class CArray;
class ACustomLabelList :
public ADataBrowser
{
public:
ACustomLabelList(
ControlRef inControl,
bool inOwner = false)
: ADataBrowser(inControl, inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
ACustomLabelList(
WindowRef inOwnerWindow,
const ControlID &inID,
bool inOwner = false)
: ADataBrowser(inOwnerWindow,inID,inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
ACustomLabelList(
WindowRef inOwnerWindow,
const Rect &inBounds,
DataBrowserViewStyle inStyle)
: ADataBrowser(inOwnerWindow, inBounds, inStyle), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
virtual ~ACustomLabelList();
void Initialize(CArray *inArray, const TextStyle &inStyle);
CFStringRef GetCurrentSelectionLabelString();
void SetCurrentSelectionLabelString(CFStringRef inString);
void SetLabelStringAt(CFStringRef inString, DataBrowserItemID inRowID);
void ShiftCurrentSelectionUp();
void ShiftCurrentSelectionDown();
void SendSelectionChangedEvent();
CFStringRef * GetLabelList() { return mCustomLabels; }
void GetLabelAt(Str15 outString, UInt32 inIndex);
protected:
virtual void ItemNotification(
Item &inItem,
DataBrowserItemNotification inMessage,
ItemData &inItemData);
virtual OSStatus GetItemData(
Item &inItem,
DataBrowserPropertyID inProperty,
ItemData &inItemData);
virtual OSStatus SetItemData(
Item &inItem,
DataBrowserPropertyID inProperty,
ItemData &inItemData);
CFStringRef mCustomLabels[kMaxLevelCount];
TextEncoding mEncoding;
private:
ACustomLabelList(const ACustomLabelList&);
ACustomLabelList& operator=(const ACustomLabelList&);
};
It looks like you're not including the file that declares std::memset.