#include "editor.cpp"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDesktopWidget *screen = QApplication::desktop();
int height = screen->height();
int width = screen->width();
textEditor window;
window.resize(width, height);
window.show();
window.resize(width/2, height);
window.show();
return app.exec();
}
Here why the width of window is n't becoming half?
On the other hand if i reverse the resize lines like this i still get a full size app.
window.resize(width/2, height);
window.show();
window.resize(width, height);
window.show();
here's the full code https://github.com/vinnusingla/TextEditor-in-QT
Related
This works fine:
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCR_WIDTH, SCR_HEIGHT, SDL_WINDOW_OPENGL);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GLContext mainContext = SDL_GL_CreateContext(window);
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
SDL_Event event;
int running = 1;
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
...
return 0;
}
But if I wrap the initialization into a class I get an access violation when I call glCreateShader even though I'm not getting any error messages when initializing SDL and OpenGL:
int main(int argc, char** argv)
{
WindowManager wm;
if (!wm.Construct())
return 0;
SDL_Event event;
int running = 1;
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); //<---- access violation here
...
return 0;
}
Here is my WindowManager class with the relevant functions:
class WindowManager
{
public:
WindowManager() = default;
bool Construct()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0);
return false;
mWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
height, SDL_WINDOW_OPENGL);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
mContext = SDL_GL_CreateContext(mWindow);
if (!mWindow)
{
spdlog::error("WindowManager could not be created!");
return false;
}
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{
spdlog::error("Failed to initialize GLAD!");
return false;
}
return true;
}
SDL_Window* mWindow;
SDL_GLContext mContext;
};
If anyone could some shine some light on what the issue could be I would greatly appreciate it!
Since I was compiling WindowManager into a dll and using it in a separate project, I essentially made two programs.
Program A had an initialized OpenGL context but Program B that was using WindowManager did not.
So I had to write a function to expose the glProcAddress to Program B in order to make raw OpenGL calls such as glCreateShader().
I don't why SDL_FINGERDOWN records
even simple touch event. It should register on Sliding finger down as name says, right? I use android N for testing.
Here is my code
#include "SDL.h"
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
int r, g, b;
r = g = b = 0;
SDL_Window *window;
window = SDL_CreateWindow("S", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 1320, SDL_WINDOW_RESIZABLE);
SDL_Surface *screen = SDL_GetWindowSurface(window);
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, NULL, white);
SDL_UpdateWindowSurface(window);
SDL_Event event;
bool running = true;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_FINGERDOWN)
{
running = false;
break;
}
//end if
} //end inner while
}
SDL_DestroyWindow(window);
SDL_Quit();
}
You have misunderstood meaning of SDL_FINGERDOWN and SDL_FINGERUP.
When you land your finger on touch screen then that's event of type SDL_FINGERDOWN.When you lift up your finger from touch screen then that's event of type SDL_FINGERUP.
I can create a SDL2 window but I don't know how to change background color of this window.
My code:
#include "SDL.h"
SDL_Window *window;
void main()
{
window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Delay(3000);
}
How can I change background color of this window to black?
You should set drawing colour with SDL_SetRenderDrawColor and then use SDL_RenderClear:
(code comes directly from SDL wiki)
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Renderer* renderer;
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
0);
// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, 0);
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
// Give us time to see the window.
SDL_Delay(5000);
// Always be sure to clean up
SDL_Quit();
return 0;
}
If you do not use SDL2 renderer you can do this:
Just call:
SDL_GL_SwapWindow(window);
After :
SDL_GL_MakeCurrent(window, context);
That's all! Now you have a black screen.
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(){
int width=512, height=512;
SDL_Window *window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
if(window==NULL)
cout << "There was an error while initializing the window" << endl << SDL_GetError << endl;
SDL_Event event;
bool running=true;
while(running){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
running=false;
break;
}
}
SDL_GetWindowSize(window, &width, &height);
SDL_Surface *surface=SDL_GetWindowSurface(window);
Uint32 skyblue=SDL_MapRGB(surface->format, 65,193,193);
SDL_FillRect(surface, NULL, skyblue);
SDL_UpdateWindowSurface(window);
}
SDL_DestroyWindow(window);
SDL_Quit();
}
Note, none of these solutions work properly with OpenGL. The question was how to init the window with predefined color, not how to paint the first frame. There's still a blink of white color before the first frame is painted.
Try this one :
SDL_Window *wind;
SDL_Surface *windSurface = NULL
void main()
{
wind = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windSurface = SDL_GetWindowSurface(wind);
SDL_FillRect(windSurface, NULL, SDL_MapRGB(windSurface->format, 240, 200, 112));
SDL_UpdateWindowSurface(wind);
SDL_Delay(3000);
}
I'm using the following code to set the background Image but as size of image is small I want to stretch the size to fit the screen or if the image is larger than screen in that case too I need the same.
Im using Gtk+3.2
#include <gtk/gtk.h>
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *layout;
GtkWidget *image;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 290, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
layout = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER (window), layout);
gtk_widget_show(layout);
image = gtk_image_new_from_file("/home/my_background_image.jpg");
gtk_layout_put(GTK_LAYOUT(layout), image, 0, 0);
button = gtk_button_new_with_label("Button");
gtk_layout_put(GTK_LAYOUT(layout), button, 150, 50);
gtk_widget_set_size_request(button, 80, 35);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I dont think you really need layout for setting background image.
You can overload "draw" signal and in handler you can paint background image.
The following code will work for you.
gboolean GtkDrawing::window_draw_cb (GtkWidget * widget, cairo_t * cr, cairo_surface_t* m_bgImage)
{
gint root_width,root_height;
cairo_set_source_surface (cr,m_bgImage, 0, 0);
gtk_window_get_size (GTK_WINDOW(widget), &root_width, &root_height);
cairo_rectangle (cr, 0, 0,root_width, root_height);
cairo_fill (cr);
//Enable Below code to draw child widget after background rendering
//gtk_widget_draw (childwidget, cr);
//cairo_fill (cr);
return TRUE;
}
cairo_surface_t* m_bgImage=cairo_image_surface_create_from_png("/home/my_background_image.png");
g_signal_connect (G_OBJECT (window), "draw", G_CALLBACK(GtkDrawing::window_draw_cb), m_bgImage);
In the above handler function, to stretch the image you can modify cairo_rectangle parameters.
I am using SDL2 in windowed mode on Windows 7 64bit to create a small game.
I get tearing at the same height of the window despite having VSYNC active. The weird thing is that the movement in itself is fluid, but there is this line, about 50px from the bottom, where tearing is noticeable. It always stays there, without moving or anything.
The weird thing is that if I crate a smaller window, I get tearing at exactly the same height from the bottom.
I tried taking screens of it but it doesn't show the tearing.
I have no idea if this is a problem of SDL2, a problem with Windows, a problem with my pc or a problem in my source file. I have tried looking online for a solution but I could not find anything like this problem.
It is a first for me since I have been playing with a lot of SDL games in the past in windowed mode and none had this problem.
Here is a snippet of the source I have been using:
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
const int SCREEN_WIDTH = 400;
const int SCREEN_HEIGHT = 300;
bool init( SDL_Window** window, SDL_Renderer** renderer ) {
SDL_Init(SDL_INIT_VIDEO);
*window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
*renderer = SDL_CreateRenderer(*window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
int flags = IMG_INIT_PNG;
IMG_Init(flags);
return true;
}
void close( SDL_Texture** image, SDL_Window** window ) {
SDL_DestroyTexture( *image );
*image = NULL;
SDL_DestroyWindow( *window );
*window = NULL;
SDL_Quit();
}
int main(int argc, char* argv[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* image = NULL;
const SDL_Rect rect = { 0, 0, SCREEN_WIDTH, 200 };
SDL_Rect rectImage, rectImageOriginal;
int w, h;
SDL_QueryTexture(image, NULL, NULL, &w, &h);
rectImage = { 0, 0, w, h };
rectImageOriginal = rectImage;
init( window, renderer );
image = IMG_LoadTexture( renderer, path );
while (running) {
[... here I move rectImage ...]
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
SDL_RenderFillRect(renderer, NULL);
SDL_RenderCopy(renderer, image, &rectImageOriginal, &rectImage);
SDL_RenderPresent(renderer);
}
close(&image, &window);
}