Draw Text to Screen - c++

So I just want to point out that I am pretty new to code outside engines so this is somewhat new to me.
I am using SDL as a base for my game and would like to know a easy way to draw text, in the form of score/time, on the screen.
So far when searching I have not found anything that I've really understood or how to use. The thing I find most when searching with the tag SDL is SDL_ttf and I've tried to look into it but with no success.
So again, I am looking for an easy way to display text, string and float/int, in the form of score/time.

Library SDL alone do not have support for writing text to the screen. Your search leads to SDL_ttf, which is right library to use.
Example of usage (only extra code, supposing you already called SDL_Init, created SDL_Window and you have SDL_Renderer* renderer for that window.
const SDL_Rect* dstrect;
SDL_Color color;
TTF_Init();
TTF_Font* font = TTF_OpenFont("font.ttf" /*path*/, 15 /*size*/);
SDL_Surface* textSurface = TTF_RenderText_Blended(font, "Text to render", color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_RenderCopy(renderer, textTexture, NULL, dstrect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
TTF_Quit();
Look into docs for other TTF_RenderText_* methods and how they differ.
And since you are using C++ (both SDL and SDL_ttf is in C), you probably want to write some wrappers around TTF rendering.

you'll want the SDL ttf library - it handles truetype fonts relatively straightforwardly.
http://www.libsdl.org/projects/SDL_ttf/
and documentation
http://jcatki.no-ip.org:8080/SDL_ttf/
http://jcatki.no-ip.org:8080/SDL_ttf/SDL_ttf.html
see especially documentation for TTF_OpenFont, TTF_CloseFont (to open & close a font file)
and the rendering functions eg TTF_RenderText_Solid
In your position I'd probably write a couple of helper functions to handle drawing text that wraps these, passing on the string, location to draw etc. and call them whenever you need to print score/etc.

Related

How to correctly draw text with Direct2D and implement outline effect

currently i am rendering text with Direct2D with following code:
IDWriteTextFormat* d2d_text_format;
m_WriteFactory->CreateTextFormat(
m_FontData.m_Font,
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
m_FontData.m_Size,
L"",
&d2d_text_format);
ID2D1SolidColorBrush* d2d_color;
m_RenderTarget->CreateSolidColorBrush(D2D1::ColorF(color.rBase(), color.gBase(), color.bBase(), color.aBase()), &d2d_color);
m_RenderTarget->BeginDraw();
m_RenderTarget->DrawText(wtext, wcslen(wtext), d2d_text_format, D2D1::RectF(x, y, 800, 600), d2d_color);
m_RenderTarget->EndDraw();
But from what i know DrawText is the less efficient method of rendering text, it works fine for me but the one problem i have is that i have trouble creating outline effect around the text, i know there's this tutorial that many people like to link but there are many problems with it, one of them is that whole project is based of active template library and the second problem i have with it is that it requires the full path to the font file unlike DrawText which just requires font family name to be defined in CreateTextFormat.
So my question would be how can i efficiently render text with Direct2D that would allow me to create an outline effect on it, do i use DrawGlyphRun? If so, how do i use that function?
This DOC may help you: https://learn.microsoft.com/en-us/windows/win32/directwrite/how-to-implement-a-custom-text-renderer
Just create your own textRenderer and pass it through pTextRenderer_:
// Draw the text layout using DirectWrite and the CustomTextRenderer class.
hr = pTextLayout_->Draw(
NULL,
pTextRenderer_, // Custom text renderer.
origin.x,
origin.y
);

C/C++ DirectX9 copy pixels at rect to another rect on the screen

Background:
Sorry for my English . So I am in a slightly unique situation in the scenario. I am working on a project that involves using a DLL proxy to intercept DirectX9 calls and dnd control drawing of a game.They are things that are statically draw and I want to be able to draw them in another part of the screen.
The Question:
I am wanting to be able to save pixels in a rect on the screen and then draw exact rect somewhere else on the screen. So if I can grab the pixels at x100, y100, w30, h30 and copy that that to another location on the screen that would be great.
This is the code that I have so far which I assume is making a texture from a memory rect.
HRESULT myIDirect3DDevice9::EndScene(void)
{
GetInput();
// Draw anything you want before the scene is shown to the user
m_pIDirect3DDevice9->GetBackBuffer(iSwapChain, iBackBuffer, Type, ppBackBuffer);
LPDIRECT3DTEXTURE9 textureMap;
D3DXCreateTexture(m_pIDirect3DDevice9, 100, 100, D3DX_DEFAULT, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &textureMap);
m_pIDirect3DDevice9->SetTexture(0, textureMap);
// SP_DX9_draw_text_overlay();
return(m_pIDirect3DDevice9->EndScene());
}
Project is based off this:
Library_Wrappers
Other notes:
I want to avoid DLL injection to accomplish this.

Explanation of SDL2 windows/surfaces?

I made a short program to test out SDL2, though there are some things I don't understand how they work.
So I have created a window and a surface:
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
Now I have this (the part I don't get):
window = SDL_CreateWindow("Window name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
So the first line: I use the SDL_createWindow() function to create a window called window I assume. The second line, I got no idea whats going on - explanation?
Finally I have this:
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
followed by some clean up code to set the pointers back to nullptr and exit the program/destroy windows etc.
The code you have pasted does the following things: Creates a SDL window called "Window name", sets its horizontal and vertical positions to center, sets the window size to 640 x 480 and marks it as shown.
The second line acquires the SDL surface bind to this window.
What this means is: Create Window , actually sets up and openGL window and a GPU texture (the Surface, althou SDL2 has seperate class for Textures), to which it is going to draw. Modifying the surface acquired with GetWindowSurface will modify the pixel on the window you have just created.
Bliting is applying a array of pixel to a target texture, in the meaning : hey i got this image/prerendered frame etc.. and I want to apply it to this surface so i can show it. Blit it.
I hope this is helpful : >
You can find more information for SDL here
Official SDL wiki
LazyFoo
LazyFoo provides a full tutorial and explanations of everything for the old SDL, but a lot of the things are the same in SDL2

Use SDL inside Irrlicht

I know you can do the same in lrrlicht, but I want to use SDL code/ functions to draw text, images inside Irrlicht (to handle 2d) and use Irrlicht to do the hardcore 3D thing, how can you apply text or images from sdl to this Irrlicht Engine, can you show me simple code, so that I can understand?
In the SDL you can do such:
// I start by declare the SDL video Name
SDL_Surface *screen;
// set the video mode:
screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; }
// I can now display data, image, text on the screen by using the declared SDL video Name " screen "
SDL_BlitSurface(my_image, &src, screen, &dest);
If you are using / targeting Windows , and are a little familiar with the WinApi, you may be able to use SDL with Irrlicht by running both inside a Win32 Window, using a combination of the SDL Window ID Hack (also see this thread) and running Irrlicht in a Win32 Window. The only problems you may face is that running SDL in a Win32 Window is extremely difficult and unpredictable.
You may be also be able to achieve a similar result using GTK+ (for cross-platform purposes), but I have personally never succeeded in running SDL or Irrlicht in a GTK+ Window.
Also, if you want a light graphics and media library like SDL , may I suggest SFML. It can run in Win32 and X/11 windows without any problems and may easily work with Irrlicht.

How to save to a bitmap in MFC C++ application?

I am just starting with MFC so please be tolerant ;).
I have wrote (it was mostly generated to be honest) a simple application which should do the Paint chores: drawing lines, rectangulars, ellipses, changing a color of object to be drawn etc.
I need to save what has been drawn on the screen into a bmp file. Any ideas how can I achieve this ?
I do not know if that's relevant but I am drawing objects on the screen without the use of any CBitmaps or things like that. Here is a part of code responsible for drawing :
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
Anchor.x=point.x;
Anchor.y=point.y;
OldPoint.x=Anchor.x;
OldPoint.y=Anchor.y;
if(pDoc->shapeCount>=MAX_SHAPES) return;
pDoc->shapeCount++;
if(bFreehand)
{
pDoc->m_shape[pDoc->shapeCount-1] = new Shape;
pDoc->m_shape[pDoc->shapeCount-1]->shape = ePoint;
}
if(bLine)
{
pDoc->m_shape[pDoc->shapeCount-1] = new CLine;
pDoc->m_shape[pDoc->shapeCount-1]->shape = eLine;
}
if(bRectangle)
{
pDoc->m_shape[pDoc->shapeCount-1] = new CRectangle;
pDoc->m_shape[pDoc->shapeCount-1]->shape = eRectangle;
}
if(bEllipse)
{
pDoc->m_shape[pDoc->shapeCount-1] = new CEllipse;
pDoc->m_shape[pDoc->shapeCount-1]->shape=eEllipse;
}
pDoc->m_shape[pDoc->shapeCount-1]->x=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->x2=point.x;
pDoc->m_shape[pDoc->shapeCount-1]->y2=point.y;
pDoc->m_shape[pDoc->shapeCount-1]->Pen=CurrentPen;
pDoc->m_shape[pDoc->shapeCount-1]->Brush=CurrentBrush;
bButtonDown=true;
SetCapture();
I have found this way to do it but I don't know how to obtain screen width and height to fill it in the CreateBitmap parameter's list
CBitmap *bitmap;
bitmap.CreateBitmap(desktopW, desktopH, 1, 32, rgbData);
CImage image;
image.Attach(bitmap);
image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatBMP);
The CreateBitmap call only requires the desktop width and height if the image you wish to save is actually the entire size of the screen. If that's indeed your intent, you can use CWnd::GetDesktopWindow() to get a CWnd object that you can query for its width and height:
http://msdn.microsoft.com/en-us/library/bkxb36k8(v=VS.80).aspx
That gets dodgy in general...if for no other reason than multi-monitor scenarios...so I'd recommend against it unless you really feel like writing a screen capture app.
What you probably want to do isn't to take a full screen shot, but just save the contents of your program's window. Typically you'd do this by breaking out the drawing logic of your program so that in the paint method you call a helper function that is written to take a CDC device context. Then you can either call that function on the window-based DC you get in the paint call or on a DC you create from the bitmap to do your save. Note that you can use a CBitmap in CDC::SelectObject:
http://msdn.microsoft.com/en-us/library/432f18e2(v=VS.71).aspx
(Though let me pitch you on not using MFC. Try Qt instead. Way better.)