Destroyed Frequently Asked Questions (markdown)

master
Ray 3 years ago
parent
commit
bfc53e5efc
1 changed files with 0 additions and 192 deletions
  1. +0
    -192
      Frequently-Asked-Questions.md

+ 0
- 192
Frequently-Asked-Questions.md

@ -1,192 +0,0 @@
This page will go over some of the common questions new users have when starting out using raylib.
__Table Of Contents__
- [How do I remove the log?](#how-do-i-remove-the-log)
- [How do I make a timer?](#how-do-i-make-a-timer)
- [What do all the fields in Camera2d mean?](#what-do-all-the-fields-in-camera2d-mean)
- [How do I center text on the screen?](#how-do-i-center-text-on-the-screen)
- [Why does calling `LoadTexture` crash my program?](#why-does-calling--loadtexture--crash-my-program)
- [How can I draw a texture flipped?](#how-can-i-draw-a-texture-flipped)
- [Why is my render texture upside down?](#why-is-my-render-texture-upside-down)
- [How do I create a depth texture?](#how-do-i-create-a-depth-texture)
- [Why is my font blurry?](#why-is-my-font-blurry)
# How do I remove the log?
Call `SetTraceLogLevel(LOG_NONE)` before `InitWindow`
# How do I make a timer?
Raylib has no built in timer system. You are expected to keep track of time in your own code. You can do with with the GetTime and GetFrameTime functions. Below is an example of a simple timer struct and functions to use it.
```c
typedef struct
{
double StartTime; // start time (seconds)
double Lifetime; // lifetime (seconds)
}Timer;
void StartTimer(Timer* timer, double lifetime)
{
timer->StartTime = GetTime();
timer->Lifetime = lifetime;
}
bool TimerDone(Timer timer)
{
return GetTime() - timer.StartTime >= timer.Lifetime;
}
double GetElapsed(Timer timer)
{
return GetTime() - timer.StartTime;
}
```
You can see a short video on the concept here. https://youtu.be/vGlvTWUctTQ
# What do all the fields in Camera2d mean?
https://youtu.be/zkjDU3zmk40
The Camera2d structure is used by `BeginMode2d/EndMode2d` to define a 2d transformation. This is very useful for games that want to draw a 2d world in a fixed coordinate system and have the ability to pan, zoom and rotate the view without the need to change drawing code.
## Fields
```c
typedef struct Camera2D {
Vector2 offset; // Camera offset (displacement from target)
Vector2 target; // Camera target (rotation and zoom origin)
float rotation; // Camera rotation in degrees
float zoom; // Camera zoom (scaling), should be 1.0f by default
} Camera2D;
```
### Offset
The offset is used to shift the window origin away from the default of the upper left corner. it is a value defined in screen space. it is not affected by rotation or zoom. It is very common to set this to be half the window width/height in order to make the window origin be the center of the screen.
### Target
This is a point in world space that the camera will follow. It defines zoom and rotation pivot points. It is very common to set this value to the thing you want to track in your world, such as your player position.
### Rotation
This is the rotation angle that the view will be rotated by. It will rotate around the target point in world coordinates.
### Zoom
This is the scale factor applied to the view. A value of 1 will draw the world at it's original scale. A value of 2 will be a 2x zoom and draw everything twice as large on screen.
## Converting Coordinates
There will be times when you need to convert from screen coordinates into world coordinates, such as wanting to find out what the mouse's screen position is in the world.
### GetScreenToWorld2D
`Vector2 GetScreenToWorld2D(Vector2 position, Camera2D camera);`
This will convert a screen point into a world point for a camera. This will include zoom and scale. It is very common to use this to get the mouse position in world coordinates to do collisions or picking.
`Vector2 mouseInWorld = GetScreenToWorld2D(GetMousePosition(), MyCamera);`
### GetWorldToScreen2D
`Vector2 GetWorldToScreen2D(Vector2 position, Camera2D camera); `
This function gets a screen point for a world point, using zoom and scale. It is useful for computing the location of HUD elements that should not be scaled or rotated with the world view, such as player names, health bars, or other labels.
# How do I center text on the screen?
Raylib does not offer any text formatting functions, so you need to compute the starting point for all text that you draw. The starting point for text is always the upper left corner.
You can compute the center of the screen by dividing the screen width and hieght in half.
```c
int screenCenterX = GetScreenWidth() / 2;
int screenCenterY = GetScreenHeight() / 2;
```
Next you need to compute how large the text is, and offset the center by half the text size.
```c
const char text[] = "Congrats! You created your first window!";
int fontSize = 20;
int textWidth = MeasureText(text, fontSize);
int textStartX = screenCenterX - textWidth / 2;
int textStartY = screenCenterY - fontSize / 2;
DrawText(text, textStartX, textStartY, fontSize, LIGHTGRAY);
```
`MeasureText` only measures the width of text, but takes fewer arguments. It is often acceptable to just use the font size as the total text height, but for some fonts, this may not be accurate. `MeasureTextEx` will measure both height and width of text, but does take more arguments. For this reason it is used less often.
# Why does calling `LoadTexture` crash my program?
You are likely calling `LoadTexture` before calling `InitWindow`.
Loading textures requires a valid OpenGL context, that `InitWindow` sets up.
Therefore, textures cannot be loaded before calling it.
# How can I draw a texture flipped?
Drawing a texture flipped requires the use of either `DrawTextureRec`, or `DrawTexturePro` functions.
Every single texture drawing function calls `DrawTexturePro` to do its work, but only these two expose the parameters necessary to flip a texture (the source rectangle).
To flip the texture, simply pass a source rectangle with a negative width or height.
```c
Rectangle source = (Rectangle) { 0, 0, -texture.width, texture.height };
DrawTextureRec(texture, source, (Vector2) { 0, 0 }, WHITE);
```
The above code will draw a texture flipped in the X axis.
# Why is my render texture upside down?
All textures in OpenGL by default have the origin in the lower-left corner, while the screen origin is in the upper left. When you load a normal texture, raylib flips the image data for you, however this cannot be done with a render texture.
The solution is to draw your render texture vertically flipped, see the above paragraph for how to do this.
# How do I create a depth texture?
By default `LoadRenderTexture()` uses a `RenderBuffer` for the depth texture, this is done for optimization (supposedly GPU can work faster if it knows that depth texture does not need to be read back). But a `RenderBuffer` is unsuitable to be drawn on the screen like a regular `Texture2D`.
Here some code to load a `RenderTexture2D` that uses a `Texture2D`for the depth:
```c
RenderTexture2D LoadRenderTextureWithDepthTexture(int width, int height)
{
RenderTexture2D target = {0};
target.id = rlLoadFramebuffer(width, height); // Load an empty framebuffer
if (target.id > 0)
{
rlEnableFramebuffer(target.id);
// Create color texture (default to RGBA)
target.texture.id = rlLoadTexture(NULL, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
target.texture.width = width;
target.texture.height = height;
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
target.texture.mipmaps = 1;
// Create depth texture
target.depth.id = rlLoadTextureDepth(width, height, false);
target.depth.width = width;
target.depth.height = height;
target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
target.depth.mipmaps = 1;
// Attach color texture and depth texture to FBO
rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
// Check if fbo is complete with attachments (valid)
if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
rlDisableFramebuffer();
}
else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
return target;
}
```
Now the `RenderTexture` depth texture can be drawn as a regular texture... but note that information contained is not normalized!
# Why is my font blurry
If you call LoadFont, the text will be loaded at a fixed size. If you draw this text at anything other than this fixed size (32 for TTF fonts), the font texture will be scaled. Scaling introduces artifacts. Scaling up will introduce blur. Load your font with LoadFontEx and specify the size you want to use it at for the best possible quality. You can pass in NULL for `fontChars `and 0 for `glyphCount` to load the default character set.
```c
LoadFontEx("myfont.ttf", 50, NULL, 0);
```

Loading…
Cancel
Save