Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

22.05.2007, 16:43

Cairo surface auf SDL Surface abbilden.

Moin,

ich versuche gerade mich in Cairo einzuarbeiten. Da ich gerne Plattformunabhängig arbeite habe ich mir überlegt das ich ja einfach die SDL fensterverwaltung verwenden kann, da cairo ja keine plattformübergreifende Möglichkeit dazu bietet. Nun habe ich mir ein kleines Test programm geschrieben, aber es passiert nicht das was ich will. Es sollte eigentlich nur ein rechteck auf den Schirm zeichnen. Vll könnt ihr mir ja helfen.

main.cpp:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>

#include <string>
#include <iostream>
using namespace std;

#include <SDL/SDL.h>
#include <cairo/cairo.h>

#include "init.hpp"
#include "util.hpp"

int main()
{
    SDL_Surface* mainSurface;
    
    mainSurface = Initialize(1280, 1024);
    
    FillSurface(mainSurface, 0x00ffffff);
    
    cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 1280, 1024);
    cairo_t* cr = cairo_create(surface);
    
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_set_line_width(cr, 5);
    
    // Rechteck zeichnen

    cairo_move_to(cr, 100     , 100);
    cairo_line_to(cr, 1280-100, 100);
    cairo_line_to(cr, 1280-100, 1024-100);
    cairo_line_to(cr, 100     , 1024-100);
    cairo_line_to(cr, 100     , 100);
    
    cairo_paint(cr);
    
    // Daten der cairo surface auf die sdl surface übertragen.

    /* Ich dachte mir das der pointer ausreicht da ein kopieren "relativ" lange dauert. */
    mainSurface->pixels = cairo_image_surface_get_data(surface);
    
    SDL_Flip(mainSurface);
    
    SDL_Delay(5000);
    
    exit(0);
}


init.hpp:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// init.hpp


#ifndef INIT_HPP
#define INIT_HPP

#include <cstdlib>
#include <iostream>

#include <SDL/SDL.h>


SDL_Surface* Initialize(uint32_t width, uint32_t height);

SDL_Surface* Initialize(uint32_t width, uint32_t height)
{
    SDL_Surface* mainSurface = NULL;

    // Exit the application correct.

    atexit(SDL_Quit);   
    
    // Initialize the sdl library with video and timer support.

    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
    {
        cout << "Error: Couldn't initialize the SDL library!" << endl;
        cout << "SDL Error: " << SDL_GetError() << endl;
        exit(0);
   }
    
    // Set the video mode and create the main surface.

    mainSurface = SDL_SetVideoMode(width, height, 32, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF);
    if(mainSurface == NULL)
    {
        cout << "Error: Couldn't setup video mode!" << endl;
        cout << "SDL Error: " << SDL_GetError() << endl;
        exit(0);
    }

    return mainSurface;
}

#endif // INIT_HPP


util.hpp:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// util.hpp


#ifndef UTIL_HPP
#define UTIL_HPP

#include <cstdlib>

#include <SDL/SDL.h>

typedef uint32_t color_t;

uint32_t MapRGB(SDL_Surface* surface, color_t color);
void FillSurface(SDL_Surface* surface, color_t color);

uint32_t MapRGB(SDL_Surface* surface, color_t color)
{
    return SDL_MapRGB(
            surface->format, 
            (color & 0x00FF0000) >> 16, 
            (color & 0x0000FF00) >> 8, 
            (color & 0x000000FF) >> 0
        );
}

void FillSurface(SDL_Surface* surface, color_t color)
{
    SDL_Rect rect;
    
    rect.x = 0;
    rect.y = 0;
    rect.w = surface->w;
    rect.h = surface->h;
    SDL_FillRect(surface, &rect, MapRGB(surface, color));
}

#endif


Makefile:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
WX_FLAGS_SHARED  = `wx-config --cxxflags --libs`
SDL_FLAGS_SHARED = `sdl-config --cflags --libs`
SDL_FLAGS_STATIC = `sdl-config --cflags --static-libs`
CAIRO_FLAGS = -I /usr/include/cairo  -l cairo

NAME = wmsys

CPP_MAIN = main.cpp

CPP_SOURCES  = 

all:
    g++ $(SDL_FLAGS_SHARED) $(CAIRO_FLAGS) $(CPP_MAIN) $(CPP_SOURCES) -o $(NAME)
    ./$(NAME)


Ich programmiere im Moment unter Ubuntu Linux 7.04 mit gcc.

David Scherfgen

Administrator

Beiträge: 10 382

Wohnort: Hildesheim

Beruf: Wissenschaftlicher Mitarbeiter

  • Private Nachricht senden